TypeScript typed array usage

后端 未结 3 609
有刺的猬
有刺的猬 2020-12-13 16:34

I have a TypeScript class definition that starts like this;

module Entities {          

    export class Person {
        private _name: string;
        pri         


        
3条回答
  •  别那么骄傲
    2020-12-13 17:26

    The translation is correct, the typing of the expression isn't. TypeScript is incorrectly typing the expression new Thing[100] as an array. It should be an error to index Thing, a constructor function, using the index operator. In C# this would allocate an array of 100 elements. In JavaScript this calls the value at index 100 of Thing as if was a constructor. Since that values is undefined it raises the error you mentioned. In JavaScript and TypeScript you want new Array(100) instead.

    You should report this as a bug on CodePlex.

提交回复
热议问题