TypeScript · Middle · Question 29 of 75
Why does Object.keys(user) return string[] rather than (keyof User)[]?
- ✓Correct answer: At runtime the object may have other keys too
- 2It's a known TypeScript bug that will be fixed in the next version
- 3Because object keys are always strings, even numeric ones
Explanation
An object of type User may contain extra properties (the type describes a minimum), so promising (keyof User)[] would be unsafe. If you're sure, use a type assertion or a typed wrapper. It's a deliberate decision by the TypeScript team.