-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Template type fails to infer a match #48430
Comments
Concatenation with + won't produce a template literal type. You must use template strings like |
@whzx5byb Thanks for the helpful reply. I think it mainly calls out a shortcoming in the minimal example above. type EventName<T extends string> = `${T}Changed`; //From feature announcement: https://github.com/microsoft/TypeScript/pull/40336
const functionRequiresEventString = function(narrowedString: EventName<string>) {
console.log('The following string is an event string per the original example: ' + narrowedString);
}
const ensureEventName = function(stringIn: string) : EventName<string> {
if(stringIn.endsWith('Changed')) {
return stringIn;
} else {
return `${stringIn}Changed`;
}
} On the line |
Thanks for the reply; this does look like it boils down to a combination of the comment above and #46958, both of which are unexpected. I'll use casting as a workaround. |
Bug Report
π Search Terms
Template literal type narrowing fails string 2345
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
Error:
Argument of type 'string' is not assignable to parameter of type '${string}Changed'.ts(2345)
(but with backticks inside the quotes in the type).π Expected behavior
No error: The concatenated string ends in 'Changed' and should pass typechecking.
Related Issues
This is a minimal example using the first example of what should be supported, from the feature announcement, so I don't think reporting an issue in the announced functionality is a duplicate of this issue looking for gaps in what that didn't cover, and that thread is now being ignored anyway. This looks a lot like this issue reported 9/1/20 as a comment on the merged PR but is simpler than this issue around numbers. Itβs also simpler than what this commenter sought around a fixed prefix.
The text was updated successfully, but these errors were encountered: