[TypesScript] Template literal types

type Statistics = {
  [K in `${"median" | "mean"}Value`]?: number
}

Mappiing a sub type:

// let winFns: "setInterval" | "setTimeout"
type winFns = Extract<keyof Window, `set${string}`>;

 

We even get some special utility types to assist with changing case

// type T1 = "sendMouseEvent" | "sendKeyboardEvent"
type T1 = `send${Capitalize<"mouse" | "keyboard">}Event`
// type T2 = "sendMOUSEEvent" | "sendKEYBOARDEvent"
type T2 = `send${Uppercase<"mouse" | "keyboard">}Event`
// type T3 = "sendmouseEvent" | "sendkeyboardEvent"
type T3 = `send${Lowercase<"Mouse" | "keyBoard">}Event`

 

posted @ 2022-07-12 14:53  Zhentiw  阅读(27)  评论(0编辑  收藏  举报