Skip to content

Template literal expression is not assignable to matching template literal typeΒ #43243

Closed
@earthlyreason

Description

@earthlyreason

Bug Report

πŸ”Ž Search Terms

template literal types, template literal assignability

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about template literal types.

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type Templated = `${string} ${string}`;

const value1: string = "abc";
const templated1: Templated = `${value1} abc` as const;
// Type '`${string} abc`' is not assignable to type '`${string} ${string}`'.

const value2 = "abc";
const templated2: Templated = `${value2} abc` as const;

Note in the second case, where value2 is a literal string, the assignment works okay.

πŸ™ Actual behavior

Type '`${string} abc`' is not assignable to type '`${string} ${string}`'.

πŸ™‚ Expected behavior

I would expect this value to be assignable, since the statically known value type matches the template pattern.

It is also odd that the two cases above behave differently.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixedA PR has been merged for this issueSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions