microsoft / microsoft/TypeScript

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

Aperta
#55,813 3 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con gli esempi motivanti nell’issue e confronta la sintassi di asserzione !== proposta con la sintassi esistente dei tipi restituiti con is e asserts. L’ambito degli operatori e delle espressioni di tipo supportati è intenzionalmente ancora irrisolto, quindi completare il lavoro richiederebbe di definire la sintassi e la semantica, implementare il type narrowing e aggiungere una copertura adeguata del compilatore.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.