Generic constraints in TypeScript allow developers to restrict the types that can be used with generics. This powerful feature enhances type safety and enables more precise control over generic types.
When working with TypeScript Generics, you might want to limit the kinds of types that can be used as type arguments. Generic constraints provide this capability by specifying requirements for the type parameter.
To apply a constraint, use the extends
keyword followed by the type that the generic type must extend or implement:
function exampleFunction<T extends SomeType>(arg: T): T {
// Function body
return arg;
}
One common use case is to ensure that a generic type has specific properties:
interface Lengthwise {
length: number;
}
function logLength<T extends Lengthwise>(arg: T): void {
console.log(arg.length);
}
logLength("Hello"); // Works
logLength([1, 2, 3]); // Works
logLength(123); // Error: Number doesn't have a length property
You can use constraints to ensure that a type parameter is an object type:
function merge<T extends object, U extends object>(obj1: T, obj2: U): T & U {
return { ...obj1, ...obj2 };
}
const merged = merge({ name: "John" }, { age: 30 });
console.log(merged); // { name: "John", age: 30 }
Generic constraints can be combined with other TypeScript features for more sophisticated type checking:
The keyof Operator can be used with constraints to create more flexible generic functions:
function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] {
return obj[key];
}
const person = { name: "Alice", age: 25 };
console.log(getProperty(person, "name")); // "Alice"
console.log(getProperty(person, "gender")); // Error: "gender" is not in keyof person
Generic constraints are a powerful feature in TypeScript that allow for more precise and type-safe generic programming. By understanding and applying constraints effectively, developers can create more robust and flexible code while maintaining strong type checking.
As you continue to explore TypeScript, consider diving into related topics such as Mapped Types and Conditional Types to further enhance your type manipulation skills.