TypeScript · Middle · Question 49 of 75

How do you type a factory function that returns the same type it received?

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.

Further reading

Problem with a question

TypeScript · Middle · question 49

Test yourself

Take the TypeScript · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz