【发布时间】:2021-11-07 11:47:35
【问题描述】:
我是 typescript 的新手,还有很多东西要学习,但我偶然发现了这个 code,这让我对 Record 实用程序类型感到非常困惑。
此代码适用于操场
const data = async (name: string, id: number): Promise<Record<number,string>> => {
const obj = {
foo: 'bar', //shouldn't this not work since I've used 'number'?
}
return obj
}
这个没有(第二行)
const foo1: Record<string, string> = { foo: 'bar' }
const foo2: Record<number, string> = { foo: 'bar' }
这是为什么?我也不确定在“键”中应该使用哪种数据类型。试试打字稿游乐场
【问题讨论】:
标签: typescript promise