Skip to content

Suggestion: int type #195

Description

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.5

emit :

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.5

also perhaps the compiler should always infer 'number' if there is not explicit type annotation

varn=3//numbervari: int=3//int

Metadata

Metadata

Assignees

No one assigned

    Labels

    Out of ScopeThis idea sits outside of the TypeScript language design constraintsSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions