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
typeFoo={a: string;b: string;};typeBar={a: number;b: number;};typeUinon=Foo|Bar;functiontest({ a, b }: Uinon){if(typeofa==="string"){a;// a is stringb;// b is string | number}}
π Actual behavior
Inside the test function, b's type does not narrow down to string when a type is string.
why discriminated union only works with literal type?
π Search Terms
"discriminated union", "discriminated union using non-literal-type"
π Version & Regression Information
[email protected]
β― Playground Link
https://www.typescriptlang.org/play/?#code/C4TwDgpgBAYg9nKBeKBvAUFKBDAXFAZ2ACcBLAOwHMBuTKAI3yLKtoF9bRIoAhbY5Gjp4o5AK4BbehGK0sjUZOmz0HdOi7QAqhTjlB8RAB9e-WugBmY8gGNgpPVGAQiAClQ4ANAyht8O8j0ASiEsUgsoV004COxkJBQAImYKSkSQjCwsbDks+ly2VXQgA
π» Code
π Actual behavior
Inside the test function, b's type does not narrow down to string when a type is string.
why discriminated union only works with literal type?
π Expected behavior
i think b's type should be string
Additional information about the issue
https://stackoverflow.com/questions/78707864/type-narrowing-doesnt-work-when-using-a-non-type-literal-field-as-a-tag/78707873#78707873
The text was updated successfully, but these errors were encountered: