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
letx: unknown="foo";if(typeofx==="string"){// typescript now knows that this is a string from inference.lety: string=x;// perform any assignment to x;// I found this by calling x = x.substring(1), but it appears just reassigning x works.x=x;// similarly; x = "bar" still results in x being unknown. Any assignment to x results in the type inference being forgotten.// x is now unknown?// Type 'unknown' is not assignable to type 'string'.(2322)y=x;}
π Actual behavior
Typescript correctly infers that x is a string if typeof x === "string", however, it seems to forget this if x is assigned to, including trivial assignments.
π Expected behavior
x is inferred to be a string, so x = x should be a no-op -> x should still be a string. This issue in and of itself is not too important, but any assigment to x (including ones that guaranteeably return a string, like x = x.substring(1) also causes TS to forget its type inference.)
I apologise if this is known or not a bug, I could not find anything similar reported on the issue tracker.
The text was updated successfully, but these errors were encountered:
More information: This seems to only happen with x: unknown. This does not happen with other cases of type inference, such as when x: string | number, or even x: any. This is presumably a bug with unknown.
Bug Report
π Search Terms
Loss of type information, Forgotten type inference.
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
Typescript correctly infers that x is a string if
typeof x === "string"
, however, it seems to forget this if x is assigned to, including trivial assignments.π Expected behavior
x is inferred to be a string, so x = x should be a no-op -> x should still be a string. This issue in and of itself is not too important, but any assigment to x (including ones that guaranteeably return a string, like
x = x.substring(1)
also causes TS to forget its type inference.)I apologise if this is known or not a bug, I could not find anything similar reported on the issue tracker.
The text was updated successfully, but these errors were encountered: