microsoft / microsoft/TypeScript

Nullish coalescing operator but for conditional types

Offen
#58,629 2 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

🔍 Search Terms

nullish coalescing operator
conditional types

✅ Viability Checklist
⭐ Suggestion

Add syntactic sugar to allow this:

type T = A extends B ?? C

to be shorthand of:

type T = A extends B ? A : C
📃 Motivating Example

When working with large complicated types, it is often that you need to check that a type extends another type before passing it as a type argument to another type.

For example, ensuring it is an array before passing it to something that requires it to be an array:

type Foo<T> = Bar<
    Some<Really<Deep<Complex<Type<Utility<T>>>>>> extends readonly unknown[]
        ? Some<Really<Deep<Complex<Type<Utility<T>>>>>>
        : never
>;

type Bar<T extends readonly unknown[]> = ...;

The more complex the type you need to check, the more you need to repeat yourself to pass the same type in again.

This proposal aims to address that.

type Foo<T> = Bar<
    Some<Really<Deep<Complex<Type<Utility<T>>>>>> extends readonly unknown[] ?? never
>;
💻 Use Cases
  1. What do you want to use this for?

Making my type code more readable and easier to maintain.

  1. What shortcomings exist with current approaches?

The need to repeat yourself. Updates to the type need to be make in two places.

  1. What workarounds are you using in the meantime?

The long-form syntax.

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

Das Issue nennt keine Implementierungsdateien oder Tests. Beginne damit nachzuverfolgen, wie bedingte Typen geparst und geprüft werden, und ermittle anschließend die erforderlichen Syntax- und Typsystemänderungen, damit A extends B ?? C sich wie A extends B ? A : C verhält; abgeschlossen ist die Aufgabe, wenn die Kurzschreibweise akzeptiert wird und das angegebene äquivalente Typverhalten aufweist.

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
Klar beschrieben
Anfängerfreundlichkeit
30/100

Neue Issues direkt in Ihr Postfach

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