Skip to content

Template literal type resolutionΒ #55364

Closed
@Treen11

Description

@Treen11

πŸ”Ž Search Terms

mapped type template literal

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about current version

⏯ Playground Link

https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgCoE8AOECydPIDeAUMsnAFzIDkAgtacgEZXUBCDAvscQCYQIANnCgoEAexABnMMhhUAPKmQQAHpBC8pNONWQAfGk2oA+ABSqqAAwAkhVJysBKZAF4TaLLnwBtVAF0eMC9kADEARjdkJRV1CE1tal0DI1MLazsHZzcPDGw8TD9AiWlZGHCqCKiYZAB6WuQAdwALdGRecQhtMGbgbWgocSgAfiCQ0IAmKJi1DS0dPUNqY3NLZFt7Rxd3T3zfDaz-Ooa4TSbW9s7tAFs4AGtQAHNkHpRRMABXKBAXkLgXiDXTDCSDIQTASBQOCCOTAVTICGjEoyOQTSpTVxyIA

πŸ’» Code

The unexpected error is marked by a comment in the code bellow. A second comment highlights a case, where this issue does not occur, even though the two cases should be identical, as far as i understand. Thank you @mkantor for the minimalistic example. The issue has been discussed on the TS discord in various channels. The original discussion was an "ask for help" here - https://discord.com/channels/508357248330760243/1139519033809575966/1139519033809575966

interface TypeMap {
  a: 'A'
  b: 'B'
}

declare const f: <T extends 'a' | 'b'>(x: `${T}`) => TypeMap[T]

type F1 = <T extends 'a' | 'b'>(x: `${T}`) => TypeMap[T]
const f1: F1 = f // why does this error?

type F2 = <T extends 'a' | 'b'>(x: `${T}`) => TypeMap[`${T}`] // and why does making the return type a template literal fix it?
const f2: F2 = f

πŸ™ Actual behavior

On line 9 occurs -

Type '<T extends "a" | "b">(x: `${T}`) => TypeMap[T]' is not assignable to type 'F1'.
  Type 'TypeMap[`${T}`]' is not assignable to type 'TypeMap[T]'.
    Type 'TypeMap[`${T}`]' is not assignable to type 'never'.
      Type 'string' is not assignable to type 'never'.
        Type 'string' is not assignable to type 'never'.(2322)

πŸ™‚ Expected behavior

No error on line 9

Metadata

Metadata

Assignees

No one assigned

    Labels

    Possible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some cases

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions