microsoft / microsoft/TypeScript
Suggestion: Allow getters to have predicate return types
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
Suggestion
🔍 Search Terms
getter, 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
This has been requested before (e.g., #6994) but declined because at the time getters were merely an aspect of implementation. Since that is no longer the case, would there be any interest in revisiting the suggestion?
📃 Motivating Example
class Person {
name?: string;
}
class Thing {
constructor(public owner: Person | null = null) {}
get isOwned(): this is Owned<this> {
return !!this.owner;
}
}
type Owned<T extends Thing> = T & { owner: NonNullable<T['owner']> };
💻 Use Cases
I have a use case much like the above. Currently type narrowing by checking thing.owner isn't honored when thing is used as a function argument:
function doSomethingWithOwnedThing(ownedThing: Owned<Thing>) {}
declare const thing: Thing;
if (thing.owner) {
thing.owner.name; // ok
doSomethingWithOwnedThing(thing); // not ok (thing.owner is possibly `null`)
}
With the above, I can create a normal function or method that returns a type predicate or otherwise simply assert that thing is an Owned<Thing>. Given that user-defined type guards are encapsulated type assertions, and getters are shiny and neat, a boolean getter that doubles as a type guard would be nicest.
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
Inizia esaminando gli esempi di getter e type guard nell’issue, quindi confronta la discussione precedente in #6994 e la discussione di progettazione collegata in #43036. Il lavoro è completato quando il tipo restituito proposto per il getter viene accettato e il narrowing dimostrato di Owned<Thing> funziona 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à
- Tranquilla
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 42/100