Skip to content

Cannot define a matching template literal type to a defined template literal Β #43143

Description

Bug Report

πŸ”Ž Search Terms

string, template, literal, type

πŸ•— Version & Regression Information

This is the behavior in every version I tried, including Nightly

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

function getCardTitle(title: string): `test-${string}` {
    return `test-${title}`;
}

πŸ™ Actual behavior

TS infers the type string for the returned string, instead of the templated type.

πŸ™‚ Expected behavior

No error!

Activity

  1. changed the title [-]Cannot define a matching template literal type to a template literal in a function declaration[/-] [+]Cannot define a matching template literal type to a defined template literal [/+] on Mar 8, 2021
  2. EdwardDrapkin commented on Mar 8, 2021

    @EdwardDrapkin
    Author

    It appears to happen in any case where there's a literal type, even the most trivial case fails:

    const aString: `a${string}` = `a${'string'}`;
  3. RyanCavanaugh commented on Mar 8, 2021

    @RyanCavanaugh
    Member

    as const shouldn't be needed here

  4. ahejlsberg commented on Mar 24, 2021

    @ahejlsberg
    Member

    We currently only produce template literal types from template literal expressions in as const contexts. We tried template literal types for all template literal expressions in #41891, but reverted it because it was too breaky. However, a happy medium would be to give template literal types to expressions that are contextually typed by string-like types. I can't imagine that breaking anything and it would solve this and other related issues.

  5. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions