【发布时间】:2021-11-20 09:15:45
【问题描述】:
这个函数
function abc<K extends keyof any>(
param: K,
) {
const z: { [p in K]: string } = {
[param]: 'abc',
};
}
给出以下错误:
TS2322: Type '{ [x: string]: string; }' is not assignable to type '{ [p in K]: string; }'.
在打字稿中不可能有这样的事情吗? 我正在使用 Typescript 4.3.5
谢谢
何塞
【问题讨论】:
-
欢迎来到 Stack Overflow!请使用tour(您将获得徽章!),环顾四周,并阅读help center,尤其是How do I ask a good question?,我还推荐Jon Skeet 的Writing the Perfect Question。 “打字稿中不可能有这样的东西吗?” 什么东西?从代码或问题文本/标题中并不清楚您具体要做什么。
标签: typescript typescript-generics dynamic-properties