Control flow based type analysis (#8010)
letx: string|number|boolean;x="";// 1while(/*...*/){x;// 2x=foo(x);// 3x;// 4}x;// 5functionfoo(x: string): number;functionfoo(x: number): boolean;functionfoo(x: boolean): string;functionfoo(){// ...}How do you determine all the types of x?
- We are certain that
x can at least have type string.
Issues with loops.
- The type in a loop can go through an "evolution" of types.
- Have to iterate to a fixed point to get the "eventual" type of that variable.
- The compiler is not written to fully accomodate this scenario.
- Symbols are not equipped to have types that vary.
- Can create a mechanism to allow a variable whose declared type can go through iterations.
- Currently this means we do a lot more overload resolution
- Adds quite a bit of work to large projects like Monaco.
- Unclear how to handle some situations with editor scenarios.
- We will need to try this all out with existing code.
Need to introduce the notion of "incomplete types".
- Iterate until a type does not change and then "complete" the type.
What if you never hit a fixed point?
- Recall, when we see an assignment, we reduce the declared type of
x, we don't add!
Still issues with circularly dependent variables:
letx: string|number|boolean;x="";// 1while(/*...*/){x;// 2lety=foo(x);// 3x=y;// 4x;// 5}x;// 6functionfoo(x: string): number;functionfoo(x: number): boolean;functionfoo(x: boolean): string;functionfoo(){// ...}
Some other issues
- Allow comaprison to
undefined and null even if they're not in the union of types you have.
- We're disallowing defensive code even though you could be called from within JS.
- Should allow it.
Functions with this types called with new (#8109)
interfaceFooType{x: number;}functionFoo(this: FooType,x: number){this.x=x;}letfoo=newFoo(12);- Should
Foo be new-able?
Foo might be a plugin or callback with an intended this binding.- Really a new syntax should clarify the intent.
- Signatures like
function new Foo(x: number): FooType;. - 👍 for new signature syntax.
- Needs a proposal though.
Indexing with enums and string literals (#2491, #7656)
- This is a problem and we need to address it.
- Unions will likely get priority and then we want to carry that to enums.
Numeric Literal Types (#7480)
- Not drastically different form string literal types.
- Currently has some strange behavior for flow of arithmetic on types.
- Need to look at implementation.
Control flow based type analysis (#8010)
How do you determine all the types of
x?xcan at least have typestring.Issues with loops.
Need to introduce the notion of "incomplete types".
What if you never hit a fixed point?
x, we don't add!Still issues with circularly dependent variables:
Some other issues
undefinedandnulleven if they're not in the union of types you have.Functions with
thistypes called withnew(#8109)Foobenew-able?Foomight be a plugin or callback with an intendedthisbinding.function new Foo(x: number): FooType;.Indexing with enums and string literals (#2491, #7656)
Numeric Literal Types (#7480)