Search code examples
typescriptobjecttypes

typescript type for object with unknown keys, but only numeric values?


I have an object that I get from an external source at runtime. It has arbitrary string string valued keys, but only numric values. It could look like:

{foo:1, bar:3} 

but it might as well look like

{asdf: 1}

I want the typescript compiler to know that for all present keys, the value is numeric. I tried things like type numObj = {string: number} and type numObj = {[string]: number} but neither works.

What is the suiting type declaration?


Solution

  • As given by jonrsharpe in his comment typescript type for object with unknown keys, but only numeric values?:

    { [key: string]: number }? Read the docs on indexable types: www.typescriptlang.org/docs/handbook/interfaces.html