microsoft / microsoft/TypeScript

Support Implicit Generics Inference

Aperta
#38,591 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.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

Search Terms

implicit generics, broader type inference, better type inference

Suggestion

I'd like TypeScript to know when two types are unknown, but equal.
That is, say I've function equals():

type T = { type: 'a', p1: boolean } | { type: 'b', p2: string };

function equals(a: T, b: T) {
  // Currently, we have to write:
  if (a.type === 'a' && b.type === 'a') { return a.p1 === b.p1;  }
  if (a.type === 'b' && b.type === 'b') { return a.p2 === b.p2; }
  return false;

  // But I'd like to be able to write:
  if (a.type !== b.type) { return false; }
  return a.type === 'a' ? a.p1 === b.p1 : a.p2 === b.p2;
}

Use Cases

I can't think about other use cases except comparisons (if you know let me know), but I think they're common enough. This will also make them somewhat faster (no need to check both's type).

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

Non sono nominati file del repository, test o entry point. Inizia riproducendo l’esempio equals in TypeScript e determina il comportamento di narrowing previsto quando i discriminanti sono uguali. Il lavoro è completato quando l’inferenza implicita richiesta funziona senza compromettere il comportamento esistente del type-checking, con test che coprono l’esempio.

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.