microsoft / microsoft/TypeScript
Allow usage of `Reflect.get()` by returning `unknown` instead of `any`
未关闭
还没有人认领这个 Issue。
Awaiting More Feedback
Suggestion
- 主要语言
- Go
- 星标
- 111k
- 派生
- 14.3k
- 平均合并
- 2 天 4 小时
- 30 天内合并 PR
- 132
描述
Search Terms
Reflect get unknown
Suggestion
Change the type definition of Reflect.get() to return unknown instead of any.
Use Cases
Allow type safe dynamic access of fields on an object or { [k: string}: unknown }
Using Reflect.get(o, 'some key') and have it return unknown.
This allows usage of Reflect.get() in code bases where we do not want implicit any and where we have @typescript-eslint rules to ban unsafe usage of any.
Examples
/**
* @param {CustomError} cause
* @returns {{ [k: string]: unknown }}
*/
function getInfoForPlainError (cause) {
/** @type {{ [k: string]: unknown }} */
const info = {}
for (const field of PLAIN_ERROR_FIELDS) {
const v = reflectGet(cause, field)
if (typeof v !== 'undefined') {
info[field] = v
}
}
return info
}
Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
Unfortunately changing a signature from any to unknown is a breaking change.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先定位 Reflect.get 的 lib.d.ts 声明以及涵盖其推断返回类型的测试。检查将 any 更改为 unknown 如何影响现有的类型检查预期;当声明和相关测试一致支持所请求的返回类型时,即表示完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- typescript
- 领域
- compilers
- Issue 类型
- 功能
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100