乐闻世界logo
搜索文章和话题

What is the keyof Operator in TypeScript?

浏览0
2月7日 16:41

keyof type operator in TypeScript is used to obtain all keys of an object type, and its return value is a union type of these keys. For example, if you have an interface:

typescript
interface Person { name: string; age: number; }

Using keyof Person yields a type that is 'name' | 'age', indicating that the return type can be either name or age. This is particularly useful when working with generic programming based on object property names, such as ensuring that function parameters are indeed keys of a specific object.

标签:TypeScript