 Constraining Generics with Extends
Constraining Generics with Extends
When working with generics in TypeScript, there are times when you need to ensure that a generic type meets certain requirements. You can achieve this by using the extends keyword to constrain the generic type. This approach allows you to specify that a generic type parameter must have a particular structure or inherit from a specific type. By constraining generics, you make your code more robust and help prevent runtime errors by catching type mismatches at compile time.
1234567function logLength<T extends { length: number }>(item: T): void { console.log(`Length is: ${item.length}`); } logLength("hello"); // Output: Length is: 5 logLength([1, 2, 3]); // Output: Length is: 3 // logLength(42); // Error: Argument of type 'number' is not assignable to parameter of type '{ length: number; }'
By using T extends { length: number }, you ensure that the function only accepts arguments that have a length property, such as strings and arrays. This constraint means that if you try to call logLength with a value that does not have a length property, TypeScript will display a compile-time error. This improves type safety by preventing accidental misuse of the function. Constraints like these are especially useful when you want to write generic code that works with a family of related types, but not with every possible type.
Tak for dine kommentarer!
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat
Awesome!
Completion rate improved to 9.09 Constraining Generics with Extends
Constraining Generics with Extends
Stryg for at vise menuen
When working with generics in TypeScript, there are times when you need to ensure that a generic type meets certain requirements. You can achieve this by using the extends keyword to constrain the generic type. This approach allows you to specify that a generic type parameter must have a particular structure or inherit from a specific type. By constraining generics, you make your code more robust and help prevent runtime errors by catching type mismatches at compile time.
1234567function logLength<T extends { length: number }>(item: T): void { console.log(`Length is: ${item.length}`); } logLength("hello"); // Output: Length is: 5 logLength([1, 2, 3]); // Output: Length is: 3 // logLength(42); // Error: Argument of type 'number' is not assignable to parameter of type '{ length: number; }'
By using T extends { length: number }, you ensure that the function only accepts arguments that have a length property, such as strings and arrays. This constraint means that if you try to call logLength with a value that does not have a length property, TypeScript will display a compile-time error. This improves type safety by preventing accidental misuse of the function. Constraints like these are especially useful when you want to write generic code that works with a family of related types, but not with every possible type.
Tak for dine kommentarer!