Featured image of post TypeScript Snippet

TypeScript Snippet

TypeScript Snippet

keyof

1
2
3
4
5
6
7
8
9
type SettingOptions = {
	fontSize: number
	lineHeight: number
	letterSpacing: number
	color: `#${string}`
	backgroundColor: `#${string}`
}

type OptionKey = keyof SettingOptions // 'fontSize' | 'lineHeight' | 'letterSpacing' ...

docs: keyof

static method of class

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class A {
	declare propertyA: string
	declare propertyB: number

	static build() {
		let a = new A()
		a.propertyA = 'default'
		a.propertyB = '100'
		return a
	}
}

valueof

1
2
3
4
5
6
7
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'
好想養貓阿~~
使用 Hugo 建立
主題 StackJimmy 設計