Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте What Are Generics? | Working with Generics
Working with Interfaces and Generics in TypeScript

bookWhat Are Generics?

In TypeScript, generics allow you to write flexible, reusable code that works with a variety of types while preserving type safety. Generics act as placeholders for types, letting you write functions, interfaces, and classes that can operate on any data type without sacrificing the benefits of static typing. This means you can avoid duplicating code for each specific type, and your code can adapt to different data types as needed. By using generics, you ensure that type information is maintained throughout your code, making it easier to catch errors at compile time and enabling better code reuse.

123
function identity<T>(arg: T): T { return arg; }
copy

The identity function is a simple example of how generics work. The function takes a type parameter T, which represents any type. The parameter arg is of type T, and the function returns a value of the same type. This means that whatever type you pass to identity, it will return a value of that same type. For instance, if you call identity with a number, the function knows to expect and return a number. If you call it with a string, it expects and returns a string. This approach preserves the type information, allowing TypeScript to provide accurate type checking and autocompletion for each usage.

1234567
function identity<T>(arg: T): T { return arg; } const num = identity<number>(42); // num is of type number const str = identity<string>("hello"); // str is of type string const obj = identity<{ x: number }>({ x: 10 }); // obj is of type { x: number }
copy
question mark

What is the main benefit of using generics in TypeScript?

Select the correct answer

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

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

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

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

Запитати АІ

expand

Запитати АІ

ChatGPT

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

Suggested prompts:

Can you explain how to use generics with interfaces or classes?

What are some real-world use cases for generics in TypeScript?

How does TypeScript infer the type parameter when calling a generic function?

Awesome!

Completion rate improved to 9.09

bookWhat Are Generics?

Свайпніть щоб показати меню

In TypeScript, generics allow you to write flexible, reusable code that works with a variety of types while preserving type safety. Generics act as placeholders for types, letting you write functions, interfaces, and classes that can operate on any data type without sacrificing the benefits of static typing. This means you can avoid duplicating code for each specific type, and your code can adapt to different data types as needed. By using generics, you ensure that type information is maintained throughout your code, making it easier to catch errors at compile time and enabling better code reuse.

123
function identity<T>(arg: T): T { return arg; }
copy

The identity function is a simple example of how generics work. The function takes a type parameter T, which represents any type. The parameter arg is of type T, and the function returns a value of the same type. This means that whatever type you pass to identity, it will return a value of that same type. For instance, if you call identity with a number, the function knows to expect and return a number. If you call it with a string, it expects and returns a string. This approach preserves the type information, allowing TypeScript to provide accurate type checking and autocompletion for each usage.

1234567
function identity<T>(arg: T): T { return arg; } const num = identity<number>(42); // num is of type number const str = identity<string>("hello"); // str is of type string const obj = identity<{ x: number }>({ x: 10 }); // obj is of type { x: number }
copy
question mark

What is the main benefit of using generics in TypeScript?

Select the correct answer

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

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

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

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