-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Template Literal Types reduces string union #54177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This sounds very much like #33471. |
As a workaround you can do this: type union = 'downcast' | 'dataDowncast' | 'editingDowncast' | `${ string & {} }Downcast`; |
It doesn't work. For example, passing the string function test( groupName: 'downcast' | 'dataDowncast' | 'editingDowncast' | `${ string & {} }Downcast` ): void {}
test('testDowncast') // Throws an error
function test2( groupName: 'downcast' | 'dataDowncast' | 'editingDowncast' | `${ string }Downcast` ): void {}
test2('testDowncast') // Works |
Oh, I see. I have a fix for this locally - might wrap it up in a PR later. I think that this should work since this |
It definitely should work - all |
Opened a PR to help with the proposed workaround: #54188 |
I'm reopening this issue per our discussion in the last design meeting. We already support the pattern type Foo = "choice1" | "choice2" | string & {}; to produce an un-reduced union for purposes of statement completion. We want to support a similar pattern for template literal types by intersecting with type Foo = "choice1" | "choice2" | `choice${string}` & {}; This means that the type in the original post should be written type union = 'downcast' | 'dataDowncast' | 'editingDowncast' | `${ string }Downcast` & {}; I will put up a PR to that effect. |
Bug Report
When the template literal type is used in a union with strings that match its pattern, the union is reduced:
This is a problem when I want to use it as a parameter type, because then only "downcast" is suggested when calling the function:
As a workaround, I can use function overloading, where one signature has union with all string literals and another only the template literal type, but it would be nice to get this issue fixed:
🕗 Version & Regression Information
From 4.1 (when template literal types were introduced) to 5.1.0-beta, tested in TypeScript Playground.
⏯ Playground Link
Link to the TypeScript Playground
🙁 Actual behavior
Only string literals that don't match the template literal type are displayed.
🙂 Expected behavior
All string literals should be shown.
The text was updated successfully, but these errors were encountered: