You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Search Terms:
variable
literal
type inference
type annotation
TS2367
TS2339
Code
typeFoo=number|"test";letx: Foo=5;lety=<Foo>5;functionbar(){x="test";y="test";}bar();if(x==="test"){// TS2367 This condition will always return 'false' since the types 'number' and 'string' have no overlap.console.log(x.length);// TS2339 Property 'length' does not exist on type 'number';}if(y==="test"){console.log(y.length);}
Expected behavior:
No error should be produced here, since the variable x was declared to be of type Foo, not number, even though it was initialized with a number literal, it's not declared as a const and thus could change (and in fact does).
Actual behavior:
The compiler seems to infer the type of x as number, from the literal 5 used to initialize it, even though the variable type is explicitly declared to be of type Foo.
Search Terms:
variable
literal
type inference
type annotation
TS2367
TS2339
Code
Expected behavior:
No error should be produced here, since the variable
x
was declared to be of typeFoo
, notnumber
, even though it was initialized with anumber
literal, it's not declared as aconst
and thus could change (and in fact does).Actual behavior:
The compiler seems to infer the type of
x
asnumber
, from the literal5
used to initialize it, even though the variable type is explicitly declared to be of typeFoo
.Playground Link:
Playground
Related Issues:
#26979
#10195
The text was updated successfully, but these errors were encountered: