TypeScript · Senior · Question 46 of 75
How does T extends object differ from T extends {} as a generic constraint?
- ✓Correct answer:
object- non-primitives,{}- everything except null/undefined - 2It's the same constraint written with different syntax
- 3
{}allows only empty objects without properties
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.