microsoft / microsoft/TypeScript
Incremental type narrowing, enabling gradual object initialization
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
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.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Das Issue nennt keine Implementierungsdatei, keinen Test und keinen Compiler-Einstiegspunkt. Beginne mit der Überprüfung des bestehenden Narrowing-Verhaltens für die angegebenen Beispiele zur Objektinitialisierung und Variablenzuweisung; als erledigt würde ein abgestimmtes Design und eine Testabdeckung für das angeforderte Assignment-Narrowing gelten.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 25/100