使用枚举限制类型字符串的多个对象侧孔
我正在尝试使用枚举来限制一个名为“ Joboffers”的对象的某些属性:
/seiority_levels.d.t.ts
export enum seniority {
JUNIOR = "Júnior",
MIDLEVEL = "Midlevel"
SENIOR = "Senior",
TECNICIAN = "Tecnician",
TRAINEE = "Trainee",
ESPECIALIST = "Especialist",
}
export interface ISeniority {
seniority: SeniorityLevels
}
/work_schedule_type.d.t.ts
export enum WorkScheduleTypes {
FullTime = 'Full-Time',
PartTime = 'Part-Time',
Flexible = 'Flexible' | 'Flexible Hours',
Other = 'Other',
}
export interface IWorkScheduleType {
workScheduleType: WorkScheduleTypes
}
,我希望我的对象具有以下其他属性:
/job_offers.ts: /job_offers.ts
import { ISeniority, seniority } from "interafces/seniority_levels";
import { IWorkScheduleType } from "interafces/work_schedule_type";
import { IStatus } from "interafces/status";
export class JobOffers implements ISeniority, IWorkScheduleType {
id: number;
title: string;
seniority: ISeniority;
workScheduleType: IWorkScheduleType;
openSince: Date;
details?: string;
}
。上面,我试图将两个属性“资历”和“ WorksCheduleType”限制在我的枚举中所存在的值。
但是,当尝试初始化这些值时,我会收到类型错误ts(2322)。
job_offers: JobOffers[] = [
{
id: 1,
title: "Data Engineering Specialist",
seniority: "Specialist",
workScheduleType: "Full-time",
openSince: new Date()
},
];
我最接近这样做的是这样的字符串枚举:
export type WorkScheduleTypes =
'Full-Time'
| 'Part-Time'
| 'Flexible' | 'Flexible Hours'
| 'Other';
但是,我想以另一种方式从使用它中受益。是否知道为什么会发生这种情况以及如何改善它?
好;)
I am trying to use enums to restrict some properties of an object called JobOffers as it follows:
/seiority_levels.d.ts
export enum seniority {
JUNIOR = "Júnior",
MIDLEVEL = "Midlevel"
SENIOR = "Senior",
TECNICIAN = "Tecnician",
TRAINEE = "Trainee",
ESPECIALIST = "Especialist",
}
export interface ISeniority {
seniority: SeniorityLevels
}
/work_schedule_type.d.ts
export enum WorkScheduleTypes {
FullTime = 'Full-Time',
PartTime = 'Part-Time',
Flexible = 'Flexible' | 'Flexible Hours',
Other = 'Other',
}
export interface IWorkScheduleType {
workScheduleType: WorkScheduleTypes
}
I would like my object to have among other properties the following:
/job_offers.ts
import { ISeniority, seniority } from "interafces/seniority_levels";
import { IWorkScheduleType } from "interafces/work_schedule_type";
import { IStatus } from "interafces/status";
export class JobOffers implements ISeniority, IWorkScheduleType {
id: number;
title: string;
seniority: ISeniority;
workScheduleType: IWorkScheduleType;
openSince: Date;
details?: string;
}
As seen above, I am trying to restrict both properties "seniority" and "workScheduleType" to the values present on my enums.
However, when trying to initialize those values, I receive a type error ts(2322).
job_offers: JobOffers[] = [
{
id: 1,
title: "Data Engineering Specialist",
seniority: "Specialist",
workScheduleType: "Full-time",
openSince: new Date()
},
];
The closest I've got this to work was string enumeration like this:
export type WorkScheduleTypes =
'Full-Time'
| 'Part-Time'
| 'Flexible' | 'Flexible Hours'
| 'Other';
However, I would like to benefit from using it the other way. Any idea of why this is happening and how to improve it?
Be nice ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论