Typescript WTF Moments 4: never extends `${infer P}` yield different result than never extends `${infer P}/`
Acid Coder
Posted on August 8, 2022
type a = never extends `${infer P}` ? P : 1 // never
// ^?
type b = never extends `${infer P}/` ? P : 2 // string
// ^?
type a1 = never extends `${infer P extends string}` ? P : 1 // never
// ^?
type b1 = never extends `${infer P extends string}/` ? P : 2 // string
// ^?
type a2 = never extends `${infer P extends number}` ? P : 1 // never
// ^?
type b2 = never extends `${infer P extends number}/` ? P : 2 // number
// ^?
type a3 = never extends `${infer P extends null}` ? P : 1 // never
// ^?
type b3 = never extends `${infer P extends null}/` ? P : 2 // null
// ^?
type a4 = never extends `${infer P extends undefined}` ? P : 1 // never
// ^?
type b4 = never extends `${infer P extends undefined}/` ? P : 2 // undefined
// ^?
type a5 = never extends `${infer P extends boolean}` ? P : 1 // never
// ^?
type b5 = never extends `${infer P extends boolean}/` ? P : 2 // boolean
// ^?
type a6 = never extends `${infer P extends bigint}` ? P : 1 // never
// ^?
type b6 = never extends `${infer P extends bigint}/` ? P : 2 // bigint
// ^?
this is a bug
💖 💪 🙅 🚩
Acid Coder
Posted on August 8, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
programming Typescript WTF Moments 4: never extends `${infer P}` yield different result than never extends `${infer P}/`
August 8, 2022