Bunch of exercises for practicing TypeScript generics
🔷 Factory function
Task
Create a generic factory function which has type parameter and returns an instance of this type.
💡Hint
✅ Solution
function createInstance<T>(c: {new (): T}): T {
return new c();
}🔷 Generic Defaults
Task
Create a generic functions with type parameter T which returns array of type T, but if no parameter passed return array of Number.
💡Hint
Use parameter defaults
✅ Solution
Task
Create a generic Type that takes a function type and returns its return type
💡Hint
Check the type to extend the function type and use infer keyword✅ Solution
type ReturnType1<T> = T extends (...args: any[])=> infer R ? R : never