Object.keys using numbers in typescript
All keys are strings in JavaScript - just use map
:
const sizes: number[] = Object.keys(foo).map(Number);
This'll only work for numeric strings - if it's a European string involving decimals, for instance, it'll be NaN
, and that never ends well.
console.log(Number("10,7"));
Or change either of your types:
const sizes: string[] = Object.keys(foo);
Or:
type Foo = { [key: string]: string };
Object properties names are always string, if you want to convert property names to numbers on the fly, you can use map(Number)
:
const sizes: number[] = Object.keys(foo).map(Number);
Pay attention because, if your property name can't be converter to number, you'll get NaN
in your sizes array