TypeScript · Senior · Question 46 of 75

How does T extends object differ from T extends {} as a generic constraint?

Explanation

{} means "any value with properties", and strings and numbers have properties, so T extends {} accepts 'abc' and 42. To allow only objects (including arrays and functions), use object. For "anything except null/undefined", {} is exactly what you need.

Further reading

Problem with a question

TypeScript · Senior · question 46

Test yourself

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

Take this quiz