TypeScript Version: 2.4.1
Code
// strictNullChecks: trueletx : string|undefinedlety=x!typet1=typeofx!typet2=typeofy
Expected behavior:
the t1 statement should create a type of type string.
the t2 statement should create a type of type string.
Actual behavior:
the t1 statement errors with ';' semicolon expected.
the t2 statement creates a type of type string.
There appears to be no way to achieve this without defining a separate variable, or by type guarding to remove the undefined / null.
With strict null checks on, this is especially troublesome in the case of generics with extends clauses, i.e.
classFoo{}classBar<TextendsFoo>{}constx : Foo|undefinedconsty=newBar<typeofx>()// throws an exception because typeof x === Foo | undefined != FooIt would be great if the non-null assertion operator worked natively in typeof statements, or even if you could use brackets to resolve the assertion before the typeof.
TypeScript Version: 2.4.1
Code
Expected behavior:
the
t1statement should create atypeof typestring.the
t2statement should create atypeof typestring.Actual behavior:
the
t1statement errors with';' semicolon expected.the
t2statement creates atypeof typestring.There appears to be no way to achieve this without defining a separate variable, or by type guarding to remove the
undefined/null.With strict null checks on, this is especially troublesome in the case of generics with extends clauses, i.e.
It would be great if the non-null assertion operator worked natively in typeof statements, or even if you could use brackets to resolve the assertion before the typeof.