microsoft / microsoft/TypeScript

Treat `expr satisfies never;` similarly to a call to a never-returning function in CFA

Aperta
#61,004 9 commenti 25 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

🔍 Search Terms

Control flow analysys, CFA, satisfies, never, return, termination, assert

✅ Viability Checklist
⭐ Suggestion

In CFA, expr satisfies never; statements should be treated similarly to a call to a never-returning function, treating statements that follow as unreachable.

I'm suggesting that TS enhance the expr satisfies never; pattern which is one of the ways to do the exhaustiveness check. It is a very popular technique used when dealing with discriminated union types. The ways often used are:

// 1-a. assign to a never-typed variable and abort
const _exhaustivenessCheck: never = option;
throw new Error("unreachable");

// 1-b. return never instead of abort
const _exhaustivenessCheck: never = option;
return _exhaustivenessCheck;

// 2. Call a function that accepts never
assertNever(option);

// 3-a. Use the satisfies syntax and abort
option satisfies never;
throw new Error("unreachable");

// 3-b. Use the satisfies syntax and return
option satisfies never;
return option;

Currently, 1 and 3 requires either throwing or returning to terminate the current (unreachable) execution branch. On the other hand, 2 automatically terminates it (assuming that the return type of assertNever is never).

My suggestion is that 3 (option satisfies never) also work as a termination point of the current execution branch, so that the following would be possible:

// 3-c (suggested behavior). Use the satisfies syntax and that's it!
option satisfies never;
/* code here is considered unreachable */

I think expr satisfies never; has several advantages compared to others:

Also, notably, expr satisfies never; is explicit enough for the compiler to reason about as part of the CFA.

In addition, I think this is also good from a theoretical perspective; never-returning functions are currently treated as a termination point of an execution branch because such functions can never return anything. Actually, this is not specific to functions. If you somehow have a value of type never, you have proven that this code isn't actually executed. expr satisfies never; feels like a good, sensible way to declare that you have a proof.

📃 Motivating Example
type Option<T> = {
  tag: "Some";
  value: T;
} | {
  tag: "None";
}


function optionToString(option: Option<unknown>): string {
  let value: string;
  switch (option.tag) {
    case "Some": {
      value = `Some(${option.value})`;
      break;
    }
    case "None": {
      value = "None";
      break;
    }
    default: {
      option satisfies never;
      // ↑ should have the same effect as:
      // assertNever(option);
    }
  }

  // Current behavior: error because `value` may not have been assigned.
  // Suggested behavior: no error here.
  return `Option(${value})`;
}

function assertNever(value: never): never {
  throw new Error("unreachable");
}

Playground

💻 Use Cases
1. What do you want to use this for?

As shown above, for exhaustiveness checks.

2. What shortcomings exist with current approaches?

Live with less optimal (from different aspects) ways.

3. What workarounds are you using in the meantime?
assertNever(option);

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 tracciando l'analisi del flusso di controllo del compilatore TypeScript per le istruzioni satisfies e la sua gestione esistente delle chiamate che non restituiscono mai. Usa l'esempio motivante optionToString come verifica del comportamento; il lavoro è completo quando il codice dopo expr satisfies never; viene trattato come irraggiungibile senza modificare il JavaScript emesso.

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

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
30/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.