You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
typeEventName<Textendsstring>= `${T}Changed`;//From feature announcement: https://github.com/microsoft/TypeScript/pull/40336constfunctionRequiresEventString=function(narrowedString: EventName<string>){console.log('The following string is an event string per the original example: '+narrowedString);}constnameEvent=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.
whzx5byb Thanks for the helpful reply. I think it mainly calls out a shortcoming in the minimal example above.
A slightly more realistic case that can't be covered by that is here:
typeEventName<Textendsstring>= `${T}Changed`;//From feature announcement: https://github.com/microsoft/TypeScript/pull/40336constfunctionRequiresEventString=function(narrowedString: EventName<string>){console.log('The following string is an event string per the original example: '+narrowedString);}constensureEventName=function(stringIn: string) : EventName<string>{if(stringIn.endsWith('Changed')){returnstringIn;}else{return`${stringIn}Changed`;}}
On the line return stringIn there's an error that this variable isn't assignable to that narrower string type.
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.
Activity
whzx5byb commentedon Mar 25, 2022
Concatenation with + won't produce a template literal type. You must use template strings like
functionRequiresEventString(`${whatChanged}Changed`)wbt commentedon Mar 25, 2022
whzx5byb Thanks for the helpful reply. I think it mainly calls out a shortcoming in the minimal example above.
A slightly more realistic case that can't be covered by that is here:
On the line
return stringInthere's an error that this variable isn't assignable to that narrower string type.whzx5byb commentedon Mar 25, 2022
wbt
The return type of
endsWithis not a type predicate, so it doesn't narrow type as you expected. Related: #46958wbt commentedon Mar 26, 2022
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.