microsoft / microsoft/TypeScript
Allow composite values to be used in type predicates
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
Allow composite values to be used in type predicates
🔍 Search Terms
Is, composite type, complex type, composite value, complex value, type predicate, type guard
✅ Viability 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, new syntax sugar for JS, etc.)
- This feature would agree with the rest of TypeScript’s Design Goals.
⭐ Suggestion
Type predicates allow you to define functions that can tell the TypeScript compiler if a parameter is of a certain type.
I suggest that this be extended to allow type predicates to narrow the type of composite objects involving parameters.
That is, the left-hand-side of is types should be allowed to include object literals.
📃 Example
Say you have a type that involves a subset of another type:
type MyType = {property1: "static text", property2: "Hello" | "World"};
A type predicate for this would always require passing in the property1 property, even though it’s unnecessary.
Now, you can create a type predicate that only takes the value of property2, but still gurantees the result type:
function foo(text: string): {property1: "static text", property2: text} is MyType {
return ["Hello", "World"].includes(text);
};
I’ve run into the problem of being unable to do this while developing my website.
I had a tagged union:
type A = {t: "A", value: "X" | "Y"};
type B = {t: "B", value: string};
type U = A | B;
And I wanted to get a value of type A from user input, but default back to a B type if the value wasn’t
To do this, I hoped to be able to do this:
function zit(x: string): {t: "A", value: x} is A {
return ["X", "Y"].includes(x);
};
function gob(x: string): U {
return zit(x) ? {t: "A", value: x} : {t: "B", value: x};
};
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Non vengono nominati file, test o punti di ingresso. Inizia esaminando gli esempi di predicati di tipo dell’issue e il comportamento esistente del compilatore per quanto riguarda i type guard; la modifica sarebbe completa quando i tipi oggetto compositi saranno accettati nei predicati e gli scenari di narrowing mostrati funzioneranno 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à
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100