Skip to content

Template Literal Types reduces string union #54177

Description

Bug Report

When the template literal type is used in a union with strings that match its pattern, the union is reduced:

typeunion='downcast'|'dataDowncast'|'editingDowncast'| `${ string}Downcast`;// ^? type union = `${string}Downcast` | "downcast"

This is a problem when I want to use it as a parameter type, because then only "downcast" is suggested when calling the function:

functiontest(groupName: union): void{}test('')// Only "downcast" is suggested

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:

functiontest2(groupName: 'downcast'|'dataDowncast'|'editingDowncast'): void;functiontest2(groupName: `${ string}Downcast` ): void;functiontest2(groupName: string): void{}test2('')// All "downcast", "dataDowncast" and "editingDowncast" are suggested

🕗 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.

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions