Typescript Keyof Type Operator
Last Updated :
22 Aug, 2024
The TypeScript keyof operator is used to get a union of all keys in an object type. It’s useful when you want to work with the property names of an object in a type-safe way, ensuring only valid keys are used.
We can use it to define generic functions that work with any object type, without knowing the specific keys of that type. It can also be used to create read-only versions of an interface or to extract specific keys from an interface.
Syntax
type KeysOfType = keyof ObjectType;
How to use the Keyof Type Operator?
We have defined an interface "Person" with three different properties: name, age, and gender. We then define a type PersonKeys that is equal to keyof Person, which is a union type of "name" | "age" | "gender".
interface Person {
name: string;
age: number;
gender: string;
}
type PersonKeys = keyof Person;
Examples Showing Keyof Type Operator
Let's look at some examples of Keyof type operator in TypeScript. These examples will help you understand the working of Keyof type operator.
1. Accessing object properties
Example: In this example, we define an interface Person with three properties: name, age, and gender. We also define a variable person of type Person with some values.
JavaScript
interface Person {
name: string;
age: number;
gender: string;
}
const person: Person = {
name: "John",
age: 25,
gender: "male",
};
function getProperty<T, K extends keyof T>(obj: T, key: K) {
return obj[key];
}
console.log(getProperty(person, "name")); // "John"
console.log(getProperty(person, "age")); // 25
console.log(getProperty(person, "gender")); // "male"
Output:
John
25
male
Explanation:
- getProperty takes an object obj of type T and a key K, where K is a valid key of T.
- K extends keyof T, ensuring the key exists in the object.
- The function returns the value associated with obj[key].
2. Using mapped Type:
Example: In this example, we have defined an interface Person with three properties: name, age, and gender.
JavaScript
interface Person {
name: string;
age: number;
gender: string;
}
type ReadonlyPerson = {
readonly [K in keyof Person]: Person[K];
}
const person: ReadonlyPerson = {
name: "John",
age: 25,
gender: "male",
};
console.log(person.name); // "John"
console.log(person.age); // 25
console.log(person.gender); // "male"
Output:
John
25
male
Explanation:
- [K in keyof Person] creates properties with keys K and value types Person[K], but read-only.
- The type ReadonlyPerson has immutable properties derived from Person.
- A ReadonlyPerson variable cannot modify its properties.
Similar Reads
TypeScript in operator narrowing Type In this article, we will learn about the 'in' operator narrowing Type in Typescript. In TypeScript, the 'in' operator is used to narrow or refine the type of an object within a conditional statement or block. It checks whether a specific property or key exists within an object, and if it does, it na
3 min read
TypeScript Instanceof Operator The TypeScript instanceof operator checks if an object is an instance of a specified class or constructor function at runtime. It returns a boolean value: `true` if the object is an instance of the type, and `false` otherwise, ensuring robust type checking during execution.SyntaxobjectName instanceo
3 min read
TypeScript Operators TypeScript operators are symbols or keywords that perform operations on one or more operands. Below are the different TypeScript Operators:Table of Content TypeScript Arithmetic operatorsTypeScript Logical operatorsTypeScript Relational operatorsTypeScript Bitwise operatorsTypeScript Assignment oper
6 min read
TypeScript Assertions Type TypeScript Assertions Type, also known as Type Assertion, is a feature that lets developers manually override the inferred or expected type of a value, providing more control over type checking in situations where TypeScript's automatic type inference may not be sufficient.Syntaxlet variableName: As
2 min read
TypeScript Generic Object Types TypeScript Generic Object Types allow you to create flexible and reusable type definitions for objects. These generic types can work with different shapes of objects while providing type safety, ensuring your code is both robust and adaptable. They are particularly useful for creating functions or c
3 min read