microsoft / microsoft/TypeScript
Allow leading underscore for types to bypass noUnusedLocals warning
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
🔍 Search Terms
"ts-ignore", "ts-expect-error", "noUnusedLocals", "noUnusedLocals underscore", "noUnusedLocals prefix"
✅ 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 support for leading underscore in type definition name to disable tsc noUnusedLocals warning ts(6196), like it does for other things.
📃 Motivating Example
Allows for easier testing of types by providing a way to enabled noUnusedLocals for your project, and selectively opting out for local types used for testing. Prior to this, you had a couple of less than idea solutions:
- live with the warnings
- disable
noUnusedLabelsfor test code - use
@ts-ignore
Depending on the method you're using for testing your types, you might not be able to use @ts-ignore. For example, if you were relying on usage of @ts-expect-error to assert invalid usages of your types in tests, @ts-ignore would also cause typescript not to flag the type error you're trying to assert with @ts-expect-error.
For example, a test like this:
type StringKeyOf<T extends Record<string, unknown>> = T extends T
? Extract<keyof T, string>
: never;
type TestType = { s: unknown; 1: unknown; };
// NOTE: `expectFails` type will get flagged with unused warning, when `noUnusedLocals` is enabled,
// and `@ts-ignore` is no help because it also suppresses the compile error we want to validate
// @ts-expect-error - validate type error will occur
type expectFails = Expect<Equals<StringKeyOf<TestType>, 's' | 1>>;
Previously, prefixing the local type with underscore would have no affect on the warning, but now you can update the code to include a leading underscore for the type name, and the warning will go away.
// @ts-expect-error
type _expectFails = Expect<Equals<StringKeyOf<TestType>, 's' | 1>>;
💻 Use Cases
- What do you want to use this for?
- I'd like to use to in my type validation tests.
- What shortcomings exist with current approaches?
- The current approaches do not provide a very good way to avoid ending up with a lot of warnings in tests, without disabling
noUnusedLocals
- The current approaches do not provide a very good way to avoid ending up with a lot of warnings in tests, without disabling
- What workarounds are you using in the meantime?
- I'm currently disabling
noUnusedLocalsin my project's base tsconfig.json and enabling in tsconfig.build.json. This choice was made so the errors would not be present for developers working on test related code, and since the build config does not include tests, it applies the rule against production code. It results in a less than ideal developer experience though, because developers will see errors in build that don't present when viewing the source code, because vscode is using tsconfig.json.
- I'm currently disabling
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず tsc と noUnusedLocals を使って動機となる例を再現し、次に未使用のローカル型に対する診断を追跡します。その型について先頭のアンダースコアによって ts(6196) が抑制され、既存の動作と出力される JavaScript が変更されない状態になれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- 機能追加
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100