microsoft / microsoft/TypeScript
keyof on generics arbitrarily returns `string` rather than the inferred values depending on the generic
未关闭
还没有人认领这个 Issue。
Bug
Domain: Index Types
- 主要语言
- Go
- 星标
- 111k
- 派生
- 14.3k
- 平均合并
- 2 天 4 小时
- 30 天内合并 PR
- 132
描述
Bug Report
🔎 Search Terms
keyof generic string
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about it
⏯ Playground Link
Playground link with relevant code
💻 Code
export type Item = {
args?: Record<string, string>;
};
export type BaseSpec = Record<string, {
items: Item[];
}>;
export function genericFunction<Spec extends BaseSpec>(spec: Spec): keyof Spec {
return {} as any;
}
const AAAAA: `someKey` = genericFunction({
someKey: {
items: [{
args: {},
}, {
args: {
name: `foo`,
},
}],
},
});
🙁 Actual behavior
Type 'string' is not assignable to type '"someKey"'.
🙂 Expected behavior
No errors
💻 Additional Information
Weirdly, it seems to work if all args objects that are specified have exactly the same content. Removing an arg object seems to work, and it works as well if we add name: "foo" into the first arg object (or remove it from the second one).
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
打开链接的 TypeScript Playground,使用 genericFunction、BaseSpec 和不同的 args 对象重现该错误。调查为什么在这种泛型推断情况下 keyof Spec 会被扩大为 string,然后验证示例能够通过类型检查,并且推断出的结果是 "someKey"。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- typescript
- 领域
- compilers
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 45/100