我有一个在运行时从外部源获取的对象。它具有任意字符串字符串值键,但只有数字值。它可能看起来像:
{foo:1, bar:3}
但它也可能看起来像
{asdf: 1}
我希望 typescript 编译器知道对于所有存在的键,值都是数字。我尝试了 type numObj = {string: number}
和 type numObj = {[string]: number}
但都不起作用。
什么是花色类型声明?
最佳答案
正如 jonrsharpe 在他的评论中给出的 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
关于具有未知键但只有数值的对象的 typescript 类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47847561/