microsoft / microsoft/TypeScript

Support `!==` operator in `is` and `asserts` return type syntax

オープン
#55,813 コメント 3 件 リアクション 1 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Awaiting More Feedback Suggestion
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

🔍 Search Terms

unknown, assert, narrow, undefined, "is not"

✅ 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 feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ Suggestion

This looks like something that would have been asked before, but it's hard to search for.

Right now asserts is a pretty magical syntax that lets you do:

function assert(proposition: unknown): asserts proposition {
    if (!proposition) {
        throw new Error()
    }
}

function foo(value: unknown): {} | null {
    assert(value !== undefined);
    return value;
}

However, trying to express a more specialized function doesn't work

function assertNotUndefined<T>(value: T): asserts value is Exclude<T, undefined> {
    if (value === undefined) {
        throw new Error();
    }
}

function foo(value: unknown): {} | null {
    assertNotUndefined(value);
    return value;  // Doesn't work
}

This is because Exclude<unknown, undefined> is still just unknown, since unknown is not just an alias for {} | null | undefined. Assuming that won't change, how else can one express assertNotUndefined? Well, if we take the original assert function and try to retroactively rationalize its syntax as this

function assert(proposition: unknown): asserts proposition;

when called as

assert(proposition !== undefined)

(hand-waving) just specializes to this

function assert(proposition: unknown): asserts proposition !== undefined;

I don't care to bikeshed over whether the syntax should be asserts proposition is not undefined, whether the LHS or RHS operands can be flipped, whether != or other operators should exist, or if any type expressions other than primitive literals like undefined should be supported, so I'll leave that to the comments.

📃 Motivating Example
function assertNotUndefined(proposition: unknown): asserts proposition !== undefined;

function foo(value: unknown): {} | null {
    assertNotUndefined(value);
    return value;  // Works!
}
💻 Use Cases
  1. What do you want to use this for?
    A function that can exclude undefined can be composed with other functions, e.g. array.filter(isNotUndefined)
  2. What shortcomings exist with current approaches?
    There's no clean way to define such a function without ugly conditionals to handle unknown.
  3. What workarounds are you using in the meantime?
    Rewrite code in more verbose style, e.g. manually construct a new array using a for loop instead of array.filter.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

issue の動機付けとなる例から始め、提案されている !== アサーション構文を、既存の is および asserts による戻り値型構文と比較します。サポートする演算子と型式の範囲は意図的に未確定のため、完了には構文と意味論を確定し、型の絞り込みを実装し、適切なコンパイラカバレッジを追加する必要があります。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
typescript
領域
compilers
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。