Closed
Description
π 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
π» 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