 What Are Generics?
What 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.
123function identity<T>(arg: T): T { return arg; }
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.
1234567function 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 }
Bedankt voor je feedback!
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Awesome!
Completion rate improved to 9.09 What Are Generics?
What Are Generics?
Veeg om het menu te tonen
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.
123function identity<T>(arg: T): T { return arg; }
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.
1234567function 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 }
Bedankt voor je feedback!