Skip to content

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

Closed

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[/-] 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. added
    Fix Available
    on Mar 25, 2021
  6. 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

BugFix Available

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions