0
这是我曾尝试:如何在使用Typescript的构造函数中为数组赋值?
class PhraseService
phrasePosShortNames: [{ id: number, name: string }];
phrasePosNames = [
{
id: 0,
longName: '#',
shortName: '#'
},
{
id: 1,
longName: 'Noun',
shortName: '#'
},
{
id: 2,
longName: 'Verb',
shortName: 'V'
}
];
constructor() {
this.phrasePosShortNames = this.phrasePosNames.map(item => {
id: item.id,
name: item.longName
})
}
但是,这给了我一个错误:
Severity Code Description Project File Line Suppression State Error TS2322 Type 'void[]' is not assignable to type '[{ id: number; name: string; }]'. Property '0' is missing in type 'void[]'
谁能告诉我什么,我做错了什么?
谢谢。我的错误,但我仍然得到一个打字错误:错误TS2322:构建:类型'{id:number; name:string; } []'不可分配为键入'[{id:number; name:string; }]”。 –
通过在{...} –
的末尾添加[]来定义我的定义。对不起,我的回答太快了。阅读我的最新编辑。 –