Angular 具有嵌套对象的对象数组的Typescript接口
我有这样的对象数组:Angular 具有嵌套对象的对象数组的Typescript接口,angular,typescript,dependency-injection,Angular,Typescript,Dependency Injection,我有这样的对象数组: export const EXAMPLE_CONFIG: IExampleConfig = [ { urlPath: '/test/test', page: 'test', fields: { fullName: 'a', mobilePhoneNumber: 'b', emailAddress: 'c', ....... } }, { ... same as above }, ] export interface IE
export const EXAMPLE_CONFIG: IExampleConfig = [
{
urlPath: '/test/test',
page: 'test',
fields: {
fullName: 'a',
mobilePhoneNumber: 'b',
emailAddress: 'c',
.......
}
},
{
... same as above
},
]
export interface IExampleConfig {
path: string;
pageTitle: string;
fields: { [key: string]: string };
}
我创建了这样一个界面:
export const EXAMPLE_CONFIG: IExampleConfig = [
{
urlPath: '/test/test',
page: 'test',
fields: {
fullName: 'a',
mobilePhoneNumber: 'b',
emailAddress: 'c',
.......
}
},
{
... same as above
},
]
export interface IExampleConfig {
path: string;
pageTitle: string;
fields: { [key: string]: string };
}
它给了我错误:类型“{path:string;pageTitle:string;字段:{fullName:string;mobilePhoneNumber:string;emailAddress:string;emailIsOwn:string;mediasource:string;};};}|{path:string;pageTitle:string;字段:{…;};}|。。。还有7个…|{…;}[]”在类型“IExampleConfig”中缺少以下属性:路径、页面标题、字段,而不是写入此内容:
export interface IExampleConfig {
path: string;
pageTitle: string;
fields: { [key: string]: string };
}
试试这个:
export interface IExampleConfig {
path?: string;
pageTitle?: string;
fields?: { [key: string]?: string };
}
??是一个Typescript符号,它标志着界面属性是可选的似乎您的类型是错误的 这样做:
export const EXAMPLE_CONFIG: IExampleConfig[]
与此相反:
export const EXAMPLE_CONFIG: IExampleConfig
另外,如果您想使接口成为可选的,您可以使用Typescript实用程序type-Partial,而不是将整个接口变成可选的。
它接受接口,并根据使用情况将其转换为可选接口
声明:
export interface IExampleConfig {
path: string;
pageTitle: string;
fields: { [key: string]: string };
}
用法
您可以在此处阅读更多信息:
[EXAMPLE\u-CONFIG:IExampleConfig->EXAMPLE\u-CONFIG:IExampleConfig[]来指定