microsoft / microsoft/TypeScript
Permit functions that return a value to also serve as a type guard
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.3k
- 平均マージ
- 2日 4時間
- マージ済み PR(30日)
- 132
説明
Search Terms
Linear type, affine type, type guard
Suggestion
It would be very helpful to allow a function to serve as a type guard, but also return an unrelated value.
Use Cases
This can be used to express type changes as a result of mutating operations, covering some of the use cases of e.g. Rust's affine types. (See also #16148.)
Examples
Consider this example, compiled with --strictNullChecks:
type NonEmptyArray<T> = {
pop(): T;
} & Array<T>;
function isNonEmpty<T>(array: Array<T>): array is NonEmptyArray<T>;
function isNonEmpty(array: Array<unknown>): boolean {
return array.length > 0;
}
let array: string[] = ['element'];
if (isNonEmpty(array)) { // Guard gives 'array' type NonEmptyArray<string>.
const elem1: string = array.pop(); // Works. This is correct.
const elem2: string = array.pop(); // Also works, but elem2 will be undefined at runtime!
}
We could make this correct if pop() could both return a value and behave as a type guard. This isn't great syntax, but nonetheless consider if this was supported:
type NonEmptyArray<T> = {
pop(): T && this is Array<T>;
} & Array<T>;
function isNonEmpty<T>(array: Array<T>): array is NonEmptyArray<T>;
function isNonEmpty(array: Array<unknown>): boolean {
return array.length > 0;
}
let array: string[] = ['element'];
if (isNonEmpty(array)) { // Guard gives 'array' type NonEmptyArray<string>.
const elem1: string = array.pop(); // Returns a string and gives 'array' type Array<string>.
const elem2: string = array.pop(); // Doesn't compile; pop() returns 'string | undefined'!
}
Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
This can use a new, previously invalid syntax to avoid affecting any existing program.
- 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.)
There's no change in the code that's emitted; this feature would exist purely at the level of the type system.
- This feature would agree with the rest of TypeScript's Design Goals.
I believe that it would. It seems to be aligned well, in particular, with "Statically identify constructs that are likely to be errors."
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
Issue ではソースファイル、テスト、コンパイラーのエントリーポイントが指定されていません。まず TypeScript における型述語と戻り値型の処理を特定し、その後、受け入れられる構文と絞り込みの挙動を、変更を行う pop() の例も含めて定義し、新しいケースと無効なケースを網羅するコンパイラーテストを用意します。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 25/100