microsoft / microsoft/TypeScript

Incremental type narrowing, enabling gradual object initialization

Aperta
#35,086 8 commenti 8 reazioni 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

delayed initialization, gradual initialization

Suggestion

What if, on every assignment to either a value or a property, that the type of that value is narrowed? It's probably easiest explained with an example:

Use Cases

It means that this code for gradually initializing an object would just work:

type Person = { firstName: string, surname: string }

function f() : Person {
    const p : Partial<Person> = {}

    p.firstName = ""
    p.surname = ""

    return p; // Error: Type 'Partial<Person>' is not assignable to type 'Person'
}

Currently this code does not type check because the compiler doesn't change the type of p after the assignments to its properties.

My thinking is that, for any value x of type X : { a : unknown },

x.a = "some string"

would result in x having its type narrowed to:

X & { a : string }

This means that the partial initialization example would work. If it works for variables as well as properties then so would this:

function f(mode: "read" | "read/write") {
    // ... Code ...
}

let s: string;

s = "read"

f(s)

Examples

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

L’issue non indica alcun file di implementazione, test o punto di ingresso del compilatore. Inizia esaminando il comportamento di narrowing esistente per gli esempi forniti di inizializzazione degli oggetti e assegnazione delle variabili; il lavoro sarebbe considerato completato con un design concordato e una copertura dei test per il narrowing delle assegnazioni richiesto.

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.