microsoft / microsoft/TypeScript

Suggestion: type narrowing based on user-defined type guards against properties

Aperta
#35,846 2 commenti 9 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

In Discussion Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

Search Terms

user-defined type guards, type predicates, type narrowing, object properties

Suggestion

TS can narrow object types in if statements and other conditional control flows by various means. Particularly, if the object's type is a union type distinguishable by a "tag" (like Option<T> in the below example), we can narrow the type by checking the value of the tag. However, if the check for the tag is done by user-defined type guards against the property (but not the object itself), the object's type is not narrowed.

The suggestion is to make it work even if user-defined type guards are applied to object properties

Use Cases

The "tagged union" pattern is widely used in TS codebases. If tags theirselves are so complicated, we want to utilize user-defined type guards to check them.

Examples

type Option<T> = {
  type: "some"
  value: T
} | {
  type: "none"
}

const isSome = (type: "some" | "none"): type is "some" => type === "some"

declare const option: Option<number>

// Good: option is narrowed in the if block
if (option.type === "some") {
  option.value
}

// Bad: option isn't narrowed
if (isSome(option.type)) {
  // Error: Property 'value' does not exist on type 'Option<number>'.
  option.value
}

// Available workaround
const isSomeObject = <T>(option: Option<T>): option is Extract<Option<T>, { type: "some" }> => option.type === "some"

if (isSomeObject(option)) {
  option.value
}

playground

Checklist

My suggestion meets these guidelines:

  • 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, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

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 dal playground collegato e dagli esempi di Option per riprodurre la differenza tra i controlli diretti delle proprietà e i type guard definiti dall’utente. Nell’issue non sono indicati né un file di implementazione né un test, quindi individua la logica di controllo del flusso per il restringimento dei tipi e i relativi test esistenti prima di definire la modifica. Il lavoro è completato quando option.value viene accettato dopo isSome(option.type), mentre il comportamento esistente del restringimento rimane invariato.

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
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.