Skip to content

template literal types starting with ${string} don't work when the string has more than 1 characterΒ #42903

Closed

Description

@DetachHead

Bug Report Suggestion

πŸ”Ž Search Terms

template literal types string

πŸ•— Version & Regression Information

4.3.0-dev.20210221

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

⏯ Playground Link

https://www.typescriptlang.org/play?ts=4.3.0-dev.20210221#code/MYewdgzgLgBAZiEAuGADAJAb2gJwJZgDmAvlmAK4C2ARgKY7GowC8MA5AIYCMATAMxsYAeiFgQMejhA4AsAChQkWNQ44UGbFHxFSmCjXqMW7BCF4DhQgCoBPAA612AIlPmngvBBhjYHCBDxCMA5qABtHKHEoe0c2DVwCEjIqOgZUNgA6IA

πŸ’» Code

const foo: `${string}${number}` = 'a123' //no error
const bar: `${string}${number}` = 'foo123' //Type '"foo123"' is not assignable to type '`${string}${number}`'.

πŸ™ Actual behavior

Type '"foo123"' is not assignable to type '`${string}${number}`'

πŸ™‚ Expected behavior

no error

Activity

  1. changed the title [-]template literal types starting with ${string} don't work[/-] [+]template literal types starting with ${string} don't work when the string is more than 1 character[/+] on Feb 21, 2021
  2. changed the title [-]template literal types starting with ${string} don't work when the string is more than 1 character[/-] [+]template literal types starting with ${string} don't work when the string has more than 1 character[/+] on Feb 21, 2021
  3. ahejlsberg commented on Feb 23, 2021

    @ahejlsberg
    Member

    This is working as intended. The pattern matching we use in template literal type inference and assignability checking is (by design) pretty simple. From #40336:

    Type inference supports inferring from a string literal type to a template literal type. For inference to succeed the starting and ending literal character spans (if any) of the target must exactly match the starting and ending spans of the source. Inference proceeds by matching each placeholder to a substring in the source from left to right: A placeholder followed by a literal character span is matched by inferring zero or more characters from the source until the first occurrence of that literal character span in the source. A placeholder immediately followed by another placeholder is matched by inferring a single character from the source.

    For your example code to behave as you expected we'd have to scan ahead and find the point where the remainder of the string is a valid number. But valid numbers include things like 1e1, Nan, Infinity, and so on. So, we simply don't attempt that and instead always match exactly one character for a placeholder immediately followed by another placeholder.

  4. DetachHead commented on Feb 24, 2021

    @DetachHead
    ContributorAuthor

    i see. can this be changed to a feature suggestion instead of a bug then?

  5. typescript-bot commented on Feb 26, 2021

    @typescript-bot
    Contributor

    This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

  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

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions