keyof to Validate a Member's Name
This lesson shows how to use keyof to extract information from a type or an interface.
We'll cover the following
Definition and goals
keyof
is a subtype of string that allows us to extract members’ names from a type or from an interface. The main goal of keyof
is to define a set of valid members’ names. For example, you may have a public function that takes the direction as a string for convenience. Defining this parameter as a string would open the door to accepting anything. However, using the keyword keyof
followed by the type that contains a union of the four cardinal points would limit the allowed string to those four values.
Get hands-on with 1200+ tech skills courses.