将 Javascript 与 Typescript (v1.4.1) 结合使用我有一个自定义类型,如下所示:
interface WordDataForQuestion {
question : {
id : number;
vocab : string;
comment : string;
};
possibleAnswers : {
[index : number] : {
id : number;
vocab : string;
comment : string;
}
};
}
现在我想将一个元素推送到数组中possibleAnswers
nextWordData.possibleAnswers.push(
{
id : vocabs[index].id,
vocab : vocabs[index].voc_t,
comment : vocabs[index].com_t
}
);
但它给了我以下错误:
exercise.ts(69,42): error TS2339: Property 'push' does not exist on type '{ [index: number]: { id: number; vocab: string; comment: string; }; }'.
我不明白出了什么问题 -possibleAnswers
这里应该是一个支持push操作的JavaScript数组。难道我不对吗?
你快到了。您确实将其定义为带有索引器的对象。不是数组,请参见下面的示例
possibleAnswers : {
id : number;
vocab : string;
comment : string;
}[];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)