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
remove generic string type from union containing it with string literals / filter generic string out of union containing literal types
π Version & Regression Information
5.7
β― Playground Link
No response
π» Code
typeLiteralString<T>=stringextendsT ? never : T;typeTest1=LiteralString<string>;// nevertypeTest2=LiteralString<"literal">;// "literal"typeTest3=LiteralString<string|"literal">// shoud be "literal"
π Actual behavior
Test3 ends up being never
π Expected behavior
Test3 should be "literal"
Additional information about the issue
No response
The text was updated successfully, but these errors were encountered:
That's not a distributive conditional type. In string extends T, the type string is not a generic type parameter, so it is just a plain conditional type, it does not distribute across unions.
Even if it were distributive, this could never work; the type string | "literal" is eagerly reduced to string, so there is no way for Test3 to behave differently from Test1, no matter how LiteralString<T> were implemented.
See #29729; you could try things like #29729 (comment) (e.g., (string & {}) | "literal") but that could have weird side effects. Or are other things you can do, like wrapping your types in something else (e.g., [string] | ["literal"]) but again, that would involve some kind of refactoring. In any case this isn't a bug in TS and this is not a general support forum so I'll disengage. Good luck!
π Search Terms
remove generic string type from union containing it with string literals / filter generic string out of union containing literal types
π Version & Regression Information
5.7
β― Playground Link
No response
π» Code
π Actual behavior
Test3
ends up beingnever
π Expected behavior
Test3
should be"literal"
Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: