TypeScript Snippet#
keyof#
type SettingOptions = {
fontSize: number
lineHeight: number
letterSpacing: number
color: `#${string}`
backgroundColor: `#${string}`
}
type OptionKey = keyof SettingOptions // 'fontSize' | 'lineHeight' | 'letterSpacing' ...
typescript
docs: keyof ↗
static method of class#
class A {
declare propertyA: string
declare propertyB: number
static build() {
let a = new A()
a.propertyA = 'default'
a.propertyB = '100'
return a
}
}
typescript
valueof#
const key = {
a: 'KEY_A',
b: 'KEY_B',
} as const
type ValueOf<T> = T[keyof T]
type Key = ValueOf<typeof key>
// type Key = 'KEY_A' | 'KEY_B'
typescript