标题说明了一切-为什么Object.keys(x)在TypeScript 中不返回类型Array<keyof typeof x>?就是这样Object.keys做的,因此对于TypeScript定义文件作者来说,似乎很明显的疏忽是不将返回类型简单地设为keyof T。
我应该在他们的GitHub存储库上记录错误,还是继续发送PR为其进行修复?
为什么 TypeScript 4.1.3 会抱怨循环中Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ bar: string; }'. No index signature with a parameter of type 'string' was found on type '{ bar: string; }'的 foo[prop] 访问for in根据定义处理引用对象的属性?
当然,我可以摆脱将对象声明为或类似的类型检查any,{[key: string]: any}但这将再次完全摆脱类型检查。
const foo = {
bar: 'bar'
};
for (const prop in foo) {
console.log(foo[prop]);
}
Run Code Online (Sandbox Code Playgroud) typescript ×2