[Typescript] WidenLiteral<T>

type WidenLiteral<T> = T extends string | number | boolean ? ReturnType<T["valueOf"]> : T;
type Example1 = WidenLiteral<"abc"> // string
type Example2 = WidenLiteral<true> // boolean
type Example3 = WidenLiteral<"abc" | 35> // string | number

// If it's not a literal, return as it is
type Example4 = WidenLiteral<{name: "abc"}> //{name: "abc"}

 

posted @ 2023-09-20 01:29  Zhentiw  阅读(4)  评论(0编辑  收藏  举报