Skip to content

Template Literal Types reduces string union #54177

@filipsobol

Description

@filipsobol

Bug Report

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

type union = '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:

function test( 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:

function test2( groupName: 'downcast' | 'dataDowncast' | 'editingDowncast' ): void;
function test2( groupName: `${ string }Downcast` ): void;
function test2( 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

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions