Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Constraining Generics with Extends | Working with Generics
Working with Interfaces and Generics in TypeScript

bookConstraining 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.

1234567
function 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; }'
copy

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.

question mark

Which of the following best describes the effect of adding extends { length: number } to a generic type parameter in a function?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 4

Spørg AI

expand

Spørg AI

ChatGPT

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

bookConstraining 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.

1234567
function 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; }'
copy

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.

question mark

Which of the following best describes the effect of adding extends { length: number } to a generic type parameter in a function?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 4
some-alt