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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 4

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Awesome!

Completion rate improved to 9.09

bookConstraining Generics with Extends

Sveip for å vise menyen

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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 4
some-alt