Javascript Typescript:推送在自定义类型数组上不可用

Javascript Typescript:推送在自定义类型数组上不可用,javascript,arrays,typescript,Javascript,Arrays,Typescript,使用带有Typescript(v1.4.1)的Javascript,我有一个自定义类型,如下所示: interface WordDataForQuestion { question : { id : number; vocab : string; comment : string; }; possibleAnswers : { [index : number] : { id : num

使用带有Typescript(v1.4.1)的Javascript,我有一个自定义类型,如下所示:

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
应该是一个支持推送操作的JavaScript数组。我说的不对吗?

你就快到了。您确实使用索引器将其定义为对象。不作为数组,请参见下面的示例

possibleAnswers : {
        id : number;
        vocab : string;
        comment : string;
}[];

看看你的接口定义——你正在定义一个类似数组的对象,而不是一个实际的数组。非常感谢,它现在可以工作了。我真的很奇怪,因为我没有在上面看到这些信息,他们没有把[]放在接口定义之后…@mbader:它实际上在“基本类型”下: