microsoft / microsoft/TypeScript

Giving an error type to types that have failed to resolve, instead of any

未关闭
#61,089 4 条评论 2 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Experimentation Needed Suggestion
主要语言
Go
星标
111k
派生
14.3k
平均合并
2 天 4 小时
30 天内合并 PR
132

描述

🔍 Search Terms

Types that fail to import should not be typed as any. SkipLibCheck. Type resolution failure.

✅ Viability Checklist
⭐ Suggestion

Currently if TS fails to resolve a type, for example because it can't resolve an import, the type is set as any. TS will display the type by it's alias (the local name given to the type), hiding that fact that it is any underneath. any being a special type that largely disables type checking, being silently introduced, can hide code errors.

Failing imports are typically reported by TS as an error, even if using that any typed type later on doesn't produce an error. However if there is a type resolution failure within an external module with skipLibCheck turned on (which is widely used and recommended, for multiple reasons), then there is no error and an any type is silently introduced to the project types.

The suggested solution is instead type these types with an error type, working name: failed. An assignment to or from a failed type is an error, like an opposite to any.

--

I've ticked non-breaking, even though it could lead to compilation of existing code failing, because it would only be highlighting an error that previously wasn't highlighted. It's non-breaking in valid code.

There would be a degree of annoyance never-the-less in triggering compilation errors in code that previously compiled, caused by problems in external libraries. Error messages indicating where the type resolution failed would be very helpful here.

I'm putting this out for discussion, because it's difficult for me to foresee the full consequences and challenges of such a change.

📃 Motivating Example

In this simple Playground we import a type from a package with an internal resolution error, that leads a type X['c'] to be any. TS reports no errors to us and lets us perform type-irrational operations.

💻 Use Cases

Fairly obviously, it's a safeguard against mistakes in external libraries, and it would also make broken imports more obvious by showing errors at the use site not just at the import site.

This occurred to me when dealing with a typing issue in @stripe/stripe-js (1.3M weekly downloads) stripe-js#714. Just fixing the import in the index file as the maintainer suggested would have introduced this issue to that library.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从链接的 Playground 示例以及对失败导入、外部模块和 skipLibCheck 的建议讨论开始。调查当前未解析的类型如何变成 any,并确定提议的失败类型应具有的预期行为和诊断信息。完成的标准是达成一个经过同意的设计,能够处理该动机示例,同时不会在不知不觉中削弱类型检查。

由索引模型根据 Issue 内容生成。

评估

技术栈
typescript
领域
compilers
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
需要澄清
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。