Description
🔎 Search Terms
template literal not assignable
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about Common "Bugs" That Aren't Bugs
- There is an error since version 4.1.5 (I think this is when template literal types have been introduced)
⏯ Playground Link
💻 Code
type Registry = {
a: { a1: {} }
b: { b1: {} }
}
type Keyof<T> = keyof T & string
function f1<
Scope extends Keyof<Registry>,
Event extends Keyof<Registry[Scope]>
>(eventPath: `${Scope}:${Event}`) {
const [scope, event] = eventPath.split(":")
console.log(scope, event)
}
function f2<
Scope extends Keyof<Registry>,
Event extends Keyof<Registry[Scope]>
>(scope: Scope, event: Event) {
// It errors here with:
// Argument of type '`${Scope}:${Event}`' is not assignable to parameter of type '`${Scope}:${Keyof<Registry[`${Scope}`]>}`'.(2345)
f1(`${scope}:${event}`)
}
🙁 Actual behavior
When calling the function f1
with a template literal value built with the same types than the template literal type parameter, it errors with:
Argument of type '
${Scope}:${Event}
' is not assignable to parameter of type '${Scope}:${Keyof<Registry[
${Scope}]>}
'.(2345)
🙂 Expected behavior
When calling the function f1
with a template literal value built with the same types than the template literal type parameter, I would expect no error.
Additional information about the issue
The message error message has changed over time, in versions 4.1.5 and 4.2.3 it was:
Argument of type 'string' is not assignable to parameter of type 'never'. (2345)
Since version 4.3.5 it is:
Argument of type '
${Scope}:${Event}
' is not assignable to parameter of type '${Scope}:${Keyof<Registry[
${Scope}]>}
'.(2345)