microsoft / microsoft/TypeScript
Add option to throw error when `!` (NonNull expression) is used on a non-nullish expression
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
🔍 Search Terms
"cast", "non null", "nonnull", "@ts-expect-error", "strict", "error"
✅ Viability Checklist
- 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, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ Suggestion
Add in the TSConfig.json a way to tell TS to check when devs are using ! (non null expressions) on variables that are non null
📃 Motivating Example
Mimic @ts-ignore vs @ts-expect-error but for !:
In well maintained codebases, @ts-expect-error can be preferred to @ts-ignore to get rid of those as soon as they become non necessary.
A similar thinking could be applied to ! so that we can keep healthy codebases
💻 Use Cases
- What do you want to use this for?
Seeing ! in a codebase should mean that the variable can be nullish, and that we're telling TS that it isn't in practice.
It replicates @ts-expect-error but for !, at one difference: when using @ts-expect-error you can pick if you want this or @ts-ignore. With this proposal, it'd be set in the TSConfig so would get applied to all ! (but this is I think what devs would want)
- What shortcomings exist with current approaches?
In large codebases, we can't keep track of why a ! was used, and whether it's still required. So we need to come back to them, and remove them manually to see if TS is happy or not without them. We can use ESLint to warn us against the use of ! but it's a 100% process.
- What workarounds are you using in the meantime?
TypeScript-ESLint can be used to either warn / ban ! and instead push devs to use ?. or ??, but sometimes a variable is indeed never nullish, so it modifies / slows down the runtime for no real reason.
As TypeScript-ESLint can also have access to the full TS process, we could also use it to check the type of the variable on which ! is used to see if it's necessary. But this requires spawning the full TS process in ESLint, slowing it down significantly on large codebases (which is why having it directly in TS could be better)
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
提案における必要な non-null アサーションと冗長な non-null アサーションの区別を、@ts-expect-error との比較および要求された TSConfig オプションと併せて確認する。すでに nullish ではないことが分かっている式に対して ! をどのように報告するべきかを定め、実装前に有効および無効な構成のテストを定義する。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 25/100