Tag: EMCA

Typescript examples – is there any really good ones?

Typescript examples – is there any really good ones?

Learning something new is never simple – but really doesn’t help when there seems to be a huge lack of good examples.

Anyone have some proper good examples, feel free to put it into the comments.

What I’m meaning is code examples like the below –

var index = 0;
var array = [1, 2, 3];
for( let index = 0; index < array.length; index++ ) {
    console.log( array[index] );
}

console.log( index ); //0

Now while there is nothing wrong in the above, for those wishing to learn Typescript and how to do it properly it should really be this –

var index : number = 0;
var array : Array = [1, 2, 3]; 
//This makes array[0] = "bob"; give a Typescript error as you can't put a string into a number type
var arrayLength : number = array.length;
for( let index : number = 0; index < arrayLength; index++ ) {
    console.log( array[index] );
}

console.log( index ); //0

See – give those var’s types! its the point of Typescript…
I’ve not even seen good examples on the Typescript site!