[Typescript] Key remapping in mapped types by using `as` keyword V4.2

Since TypeScript v4.2, we can do remapping by using `as` keyword

type Colors = "red" | "green" | "blue"
type ColorSelector = {
    [K in Colors as `select${Capitalize<K>}`]: () => void
}
const cs: ColorSelector = {} as any
cs.selectRed()

posted @ 2022-07-13 17:48  Zhentiw  阅读(27)  评论(0编辑  收藏  举报