Reputation: 25
It never gives any array. Array accept only for one type. but tuples can allow multiple values.
var myTuple = [10,"Hello"];
console.log(myTuple[1]);
var myArray:number[] = [10,20,"Hello"];
console.log(myArray[2]);
[+] Update : my question, why doesn't it give any run time errors ?
Upvotes: 0
Views: 248
Reputation: 3253
Typescript is a statically typed language that works compile-time and it eventually converts your code to pure Javascript. the code in your post will be converted to below js code:
var myTuple = [10, "Hello"];
console.log(myTuple[1]);
var myArray = [10, 20, "Hello"]; // notice how number[] is stripped away from your code
console.log(myArray[2]);
Since Javascript is a dynamically typed language, it has no issues with myArray
. whereas in Typescript, you have specified before hand that myArray would be a number array type and TS being a statically typed language checks if the value assigned to myArray is indeed a number array or not, which in this case is not. so it raises the following error:
Type 'string' is not assignable to type 'number'.(2322)
Upvotes: 2