Skip to content

Template literal type ${string}${number}${string} errors on valid values #57355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
joslarson opened this issue Feb 9, 2024 · 2 comments
Closed
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@joslarson
Copy link

joslarson commented Feb 9, 2024

🔎 Search Terms

"${string}${number}${string}", "template literal types number between strings"

🕗 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

https://www.typescriptlang.org/play?#code/MYewdgzgLgBAtgTwMpQE4EswHMBcADAEgG9oNsBfYsAVzgCMBTVSktTLcvGAXhgHIoDaAEYATAGZB0PgG4AUAHoFMFTAB6GzRpgAVBAAcG-AERSoYyUKjG+MdBBhgQsAIYQI6LGBd0ANkagQGCgDIz5CVjIOKlpGZmJSdk4+ADooCAAKCVFRAEo5UEhYRBQo0XwEtgoY+iZOHn4zC1lFZVUtLV1QkyaJGzsHJ1d3T28-AKCQw34IxOqiGlrmPFT0rPEc-MLoeGQqrHEKhdi6yqj63j4LM1kYJRh9NwghGAAzEFQYCBA4I1QGNzgORAA

💻 Code

const myString:`${string}${number}${string}` = 'test123test';
//    ^^^^^^^^ Type '"test123test"' is not assignable to type '`${string}${number}${string}`'.ts(2322)
const myString2:`${string}${number}` = 'test123';
//    ^^^^^^^^ Type '"test123"' is not assignable to type '`${string}${number}`'.ts(2322)
const myString3:`${number}${string}` = '123test'; // passes for some reason

🙁 Actual behavior

Received error "Type '"test123test"' is not assignable to type '${string}${number}${string}'.ts(2322)" when pattern matches

🙂 Expected behavior

I expect it to match any literal string type that has a number within it

Additional information about the issue

My real use case is that I would like to be able to exclude/omit patterns like ${string}${number}${string}, but that's not possible since it doesn't match

@jcalz
Copy link
Contributor

jcalz commented Feb 9, 2024

See #57126 and essentially intentional as described in #40336:

A placeholder immediately followed by another placeholder is matched by inferring a single character from the source.

So `${string}${number}${string}` matches the first character against string, the second against number, and then the rest of the string against string. Yes, it's weird.

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Feb 9, 2024
@RyanCavanaugh
Copy link
Member

As a general principle, template string literals aren't regexes and don't have regex capabilities. Anything that requires backtracking (or extensive lookahead) isn't going to happen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants