-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Typescript的keyof的作用是什么 #153
Comments
keyof 可以得到对象上的 key 组成的联合类型。配合 extends 可以约束泛型, keyof 自身得到的类型也能约束类型。 function foo<T, K extends keyof T>(obj: T, key: K) {}
interface bar {
age: number
name: string
}
keyof bar // 'age' | 'name' |
|
keyof 操作符能让你捕获一个类型的键。 任何类型 T, keyof T的结果为 T上已知的公共属性名的联合。 |
可以通过keyof操作符在类型上下文中得到引用变量或者属性的类型。
如果是函数,结合预定义类型ReturnType,可以得到它的返回类型
如果是对象,通过typeof可以得到对象上的key组成的联合类型
配合extends关键字,可以实现对泛型的约束
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No description provided.
The text was updated successfully, but these errors were encountered: