Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте 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

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 4

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Awesome!

Completion rate improved to 9.09

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

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 4
some-alt