Description
Bug Report
π Search Terms
Template literal type narrowing fails string 2345
π Version & Regression Information
- This changed between versions 4.0.5 and 4.1.5 because that is when the feature was introduced.
β― Playground Link
Playground link with relevant code
π» Code
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 nameEvent = function(whatChanged: string) {
functionRequiresEventString('Functionality Changed');
//Next line fails: Argument of type 'string' is not assignable to parameter of type '`${string}Changed`'.ts(2345)
functionRequiresEventString(whatChanged + 'Changed');
}
π 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.