microsoft / microsoft/TypeScript

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

Offen
#55,813 3 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

🔍 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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit den motivierenden Beispielen in der Issue und vergleiche die vorgeschlagene !==-Assertion-Syntax mit der vorhandenen Syntax für Rückgabetypen mit is und asserts. Der Umfang der unterstützten Operatoren und Typausdrücke ist absichtlich noch ungeklärt; die Umsetzung würde daher voraussetzen, die Syntax und Semantik festzulegen, die Typverengung zu implementieren und eine angemessene Compiler-Abdeckung hinzuzufügen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.