TypeScript · Middle · Question 29 of 75

Why does Object.keys(user) return string[] rather than (keyof User)[]?

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.

Further reading

Problem with a question

TypeScript · Middle · question 29

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