其他分享
首页 > 其他分享> > [TypesScript] Template literal types

[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`

 

标签:TypesScript,winFns,T2,T3,send,literal,type,Event,types
来源: https://www.cnblogs.com/Answer1215/p/16470081.html