Skip to content

Template literal value not assignable to template literal type #56659

Closed
@patrick-bricout-spendesk

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

https://www.typescriptlang.org/play?#code/C4TwDgpgBAShDmBLAzsATiKBeKBvAUFEVAIYBcepAjBbgL5R2HEBGtULNeDTT+okKAGkIIAPYAzADwAVAHzYoAa1GSoMqADIoqNIgB28fPgkBXfQGNgiMfqgSqU5kQDKFsYIgAPYBH0ATZGFVaTgkXRA5ABpnKABRADc-YChvXwCgkXFQhBR0EABtNw8IAF05fDkACggk-WAABRJgAAsKAAMAElxiyDoybsTkunaASjxY931UKALkd0go1LrgUsVa5KbWgDpkMAAbRGAqgCIyE9HJ22QxfYht-bF4KvmSpY36y74zS2tbewATE5iFBetA0n5AsFslIwnkMNFYkN6qkfJDMiFYbkIkUFmUKtVXpAKGD3isKMjgOMCCCHFUurgiRB+t0PsARl98EA

💻 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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions