TypeScript · Middle · Question 49 of 75
How do you type a factory function that returns the same type it received?
- ✓Correct answer:
function clone<T>(value: T): T - 2
function clone(value: any): any - 3
function clone(value: object): object
Explanation
A generic preserves the link between input and output: clone(user) returns a User. With any and object, the calling code loses type information and has to cast.