[英]Typescript Object Class members are defined with interface
In Typescript, how do I define an Object Const, however make sure item members are in a interface?在 Typescript 中,如何定义 Object 常量,但要确保项目成员在接口中?
Currently have this:目前有这个:
export const FrequencyType = {
Weekly: { id: 1, code: 'Weekly', description: 'Weekly Description'},
Annual: { id: 2, code: 'Annual', description: 'Annual Description'}
}
Intended:故意的:
with LookupVm Interface, this is giving error使用 LookupVm 接口,这会出错
export const FrequencyType = {
Weekly: LookupVm: { id: 1, code: 'Weekly', description: 'Weekly Description'},
Annual: LookupVm: { id: 2, code: 'Annual', description: 'Annual Description'}
}
Reference:参考:
export interface LookupVm {
id: number;
code: string;
description: string;
}
Create interface like the below创建如下界面
export default interface LookupVm{
id:number,
code:string,
description: string
}
Then use it in the const like然后在 const 中使用它
export const FrequencyType :{
Weekly: LookupVm,
Annual: LookupVm
}= {
Weekly: { id: 1, code: 'Weekly', description: 'Weekly Description'},
Annual: { id: 2, code: 'Annual', description: 'Annual Description'}
}
Here is one solution, However Hitech solution will work also这是一种解决方案,但是 Hitech 解决方案也可以使用
export const FrequencyType = {
Weekly: {
id: 1,
code: 'Weekly',
description: 'Weekly Description'
} as LookupVm,
Annual: {
id: 2,
code: 'Annual',
description: 'Annual Description'
} as LookupVm
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.