{"id":384,"date":"2022-08-30T15:00:27","date_gmt":"2022-08-30T15:00:27","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/typescript-typed-array-usage-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:00:27","modified_gmt":"2022-08-30T15:00:27","slug":"typescript-typed-array-usage-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/typescript-typed-array-usage-collection-of-common-programming-errors\/","title":{"rendered":"TypeScript typed array usage-Collection of common programming errors"},"content":{"rendered":"<p>I have a TypeScript class definition that starts like this;<\/p>\n<pre><code>module Entities {          \n\n    export class Person {\n        private _name: string;\n        private _possessions: Thing[];\n        private _mostPrecious: Thing;\n\n        constructor (name: string) {\n            this._name = name;\n            this._possessions = new Thing[100];\n        }\n<\/code><\/pre>\n<p>Looks like an array of type Thing does not get translated correctly to the corresponding Javascript array type. This is a snippet from the generated Javascript:<\/p>\n<pre><code>   function Person(name) {\n            this._name = name;\n            this._possessions = new Entities.Thing[100]();\n        }\n<\/code><\/pre>\n<p>Executing code containing a Person object, throw an exception when attempting to initialize the _possession field. Error is &#8220;0x800a138f &#8211; Microsoft JScript runtime error: Unable to get value of the property &#8216;100&#8217;: object is null or undefined&#8221;.<\/p>\n<p>If I change the type of _possession to <code>any[]<\/code> and initialize _possession with <code>new Array()<\/code> exception is not thrown. Did I miss something?<\/p>\n<ol>\n<li>\n<p>You have an error in your syntax here:<\/p>\n<pre><code>this._possessions = new Thing[100]();\n<\/code><\/pre>\n<p>This doesn&#8217;t create an &#8220;array of things&#8221;. To create an array of things, you can simply use the array literal expression:<\/p>\n<pre><code>this._possessions = [];\n<\/code><\/pre>\n<p>Of the array constructor if you want to set the length:<\/p>\n<pre><code>this._possessions = new Array(100);\n<\/code><\/pre>\n<p>I have created a brief working example you can try in the playground.<\/p>\n<pre><code>module Entities {  \n\n    class Thing {\n\n    }        \n\n    export class Person {\n        private _name: string;\n        private _possessions: Thing[];\n        private _mostPrecious: Thing;\n\n        constructor (name: string) {\n            this._name = name;\n            this._possessions = [];\n            this._possessions.push(new Thing())\n            this._possessions[100] = new Thing();\n        }\n    }\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<p>The translation is correct, the typing of the expression isn&#8217;t. TypeScript is incorrectly typing the expression <code>new Thing[100]<\/code> as an array. It should be an error to index <code>Thing<\/code>, 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 <code>Thing<\/code> as if was a constructor. Since that values is <code>undefined<\/code> it raises the error you mentioned. In JavaScript and TypeScript you want <code>new Array(100)<\/code> instead.<\/p>\n<p>You should report this as a bug on CodePlex.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 18:41:17. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I have a TypeScript class definition that starts like this; module Entities { export class Person { private _name: string; private _possessions: Thing[]; private _mostPrecious: Thing; constructor (name: string) { this._name = name; this._possessions = new Thing[100]; } Looks like an array of type Thing does not get translated correctly to the corresponding Javascript array [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-384","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/384","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=384"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/384\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=384"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=384"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=384"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}