Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer 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

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 4

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

Suggested prompts:

Can you explain how to use other constraints with generics in TypeScript?

What are some common use cases for generic constraints?

Can you show more examples of constraining generics in TypeScript?

Awesome!

Completion rate improved to 9.09

bookConstraining Generics with Extends

Veeg om het menu te tonen

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

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 4
some-alt