microsoft / microsoft/TypeScript

Suggestion: Allow getters to have predicate return types

Offen
#43,368 14 Kommentare 84 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit der Prüfung der Getter- und Type-Guard-Beispiele im Issue und vergleiche anschließend die vorherige Diskussion in #6994 sowie die verlinkte Designdiskussion in #43036. Abgeschlossen ist die Aufgabe, wenn der vorgeschlagene Rückgabetyp des Getters akzeptiert wird und die demonstrierte Eingrenzung von Owned<Thing> funktioniert, ohne das erzeugte JavaScript zu ändern.

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
Ruhig
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
42/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.