Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript typed array usage

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 type. This is a snippet from the generated JavaScript:

function Person(name) {     this._name = name;     this._possessions = new Entities.Thing[100](); } 

Executing code containing a Person object, throw an exception when attempting to initialize the _possession field:

Error is "0x800a138f - Microsoft JScript runtime error: Unable to get value of the property '100': object is null or undefined".

If I change the type of _possession to any[] and initialize _possession with new Array() exception is not thrown. Did I miss something?

like image 256
Klaus Nji Avatar asked Oct 13 '12 05:10

Klaus Nji


People also ask

What is the use of typed array in JavaScript?

JavaScript typed arrays are array-like objects that provide a mechanism for reading and writing raw binary data in memory buffers. Array objects grow and shrink dynamically and can have any JavaScript value. JavaScript engines perform optimizations so that these arrays are fast.

Why might you use typed arrays instead of standard arrays?

A typed array significantly simplifies the level of proof the engine needs to be able to optimise around it. A value returned from a typed array is certainly of a certain type, and engines can optimise for the result being that type.

What is array type in TypeScript?

In typescript, an array is a data type that can store multiple values of different data types sequentially. Similar to JavaScript, Typescript supports array declaration and there are multiple ways to do it. Declaring and Initializing Arrays: We can either use var or let for declaring an array.

How do you declare an array of any type in TypeScript?

In TypeScript, an array is an ordered list of values. An array can store a mixed type of values. To declare an array of a specific type, you use the let arr: type[] syntax.


1 Answers

You have an error in your syntax here:

this._possessions = new Thing[100](); 

This doesn't create an "array of things". To create an array of things, you can simply use the array literal expression:

this._possessions = []; 

Of the array constructor if you want to set the length:

this._possessions = new Array(100); 

I have created a brief working example you can try in the playground.

module Entities {        class Thing {      }              export class Person {         private _name: string;         private _possessions: Thing[];         private _mostPrecious: Thing;          constructor (name: string) {             this._name = name;             this._possessions = [];             this._possessions.push(new Thing())             this._possessions[100] = new Thing();         }     } } 
like image 182
Fenton Avatar answered Oct 06 '22 19:10

Fenton