why object.values({})'s type is unknown[]

Viewed 36
var testObj = {};
const typeTest3 = Object.values(testObj);

Why is typeTest3's type unknown[] while Object.keys({})'s type is string[] ?

1 Answers

Because it produces an array that iterates over the values, which aren't guaranteed to have any specific type. Object.values has 1 generic type parameter though, so you can type the produced values yourself:

Object.values<number>({a: 5, b: 3}).map(x => x + 3)

The type of x will be number instead of unknown.

(PS: Object.keys produces strings because in JavaScript all object keys are coerced into a string)

Related