Junior — Middle
Have you worked with generic types in TypeScript?
sobes.tech AI
Answer from AI
Yes, generics in TypeScript allow creating components that work with different data types while maintaining type safety. This helps in writing more versatile and reusable code.
Example of a function with a generic type:
function identity<T>(arg: T): T {
return arg;
}
const num = identity<number>(42);
const str = identity<string>("hello");
Here, T is a type parameter that is substituted when the function is called.