Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading