Introducing int type could allows some error to be caught at compile time (like trying to index an array with a float) and perhaps improve performance of outputted javascript, to obtains true int, TypeScript could systematically emit a cas with |0 when a variable/parameter is an integer :
vari: int;varn: number;vara: any=1.2;i=1.1;// error;i=n;// errori=3/4;// valid, type castedi=a;// valid, type castedvarindexable: {[i: int]: bool}={};indexable[n]=true;// errorindexable[i]=true;// validindexable[i/2]=true;// valid, type castedindexable[a]=true;// valid, type castedfunctionlogInt(i: int){console.log(i);}would emit
varn;vari=i|0;vara=1.2;i=1.1// error;i=n// errori=(3/4)|0;// valid, type castedi=a|0;// valid, type castedvarindexable={};indexable[n]=true;// errorindexable[i]=true;// validindexable[(i/2)|0]=true;// valid, type castedindexable[a|0]=true;// valid, type castedfunctionlogInt(i: int){i=i|0;console.log(i);}There will perhaps be a problem with generic and method but I guess the compiler could in this case type cast when passing the parameter:
functionadd<T>(a: T,b: T): T{returna+b;}vara=add(1,2);// a is number value 3varb=add<int>(1/2,2);// b is int, value 2varc=add(1/2,2);// c is number, value 2.5emit :
functionadd(a,b){returna+b;}vara=add(1,2);// a is number value 3varb=add<int>((1/2)|0,2);varc=add(1/2,2);// c is number, value 2.5also perhaps the compiler should always infer 'number' if there is not explicit type annotation
varn=3//numbervari: int=3//int
Introducing
inttype could allows some error to be caught at compile time (like trying to index an array with a float) and perhaps improve performance of outputted javascript, to obtains trueint, TypeScript could systematically emit a cas with|0when a variable/parameter is an integer :would emit
There will perhaps be a problem with generic and method but I guess the compiler could in this case type cast when passing the parameter:
emit :
also perhaps the compiler should always infer 'number' if there is not explicit type annotation