microsoft / microsoft/TypeScript

Suggestion: shorthand for templated type to extend and default to the same type

Offen
#30,919 9 Kommentare 13 Reaktionen 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

default extends equals generics = template types is

Suggestion

Can we have a way to declare that a templated (generic) type both extends and is = a type value without repeating the type value?

declare function example<T extends Base = Base>(value: T): T;

A clean way to do this would be to allow the is keyword following a generic type:

declare function example<T is Base>(value: T): T;

Failing that, how about omitting the first type value?

declare function example<T extends = Base>(value: T): T;

Use Cases

Generic parameters with extended and default values set to the same are a reasonable pattern:

declare function querySelector<E extends Element = Element>(selectors: string): E | null;

In larger type definitions with many generics, these extends+= definitions can get a little cumbersome:

type BitAddThree<
    A extends Bit = Bit,
    B extends Bit = Bit,
    C extends Bit = Bit
> =
    [A, B, C] extends [0, 0, 0] ? [0, 0] :
    // etc. etc.
    [Bit, Bit, Bit];

Examples

declare function querySelector<E is Element>(selectors: string): E | null;
declare function querySelector<E extends = Element>(selectors: string): E | null;
type BitAddThree<A is Bit, B is Bit, C is Bit> = // ...
type BitAddThree<A extends = Bit, B extends = Bit, C extends = Bit> = // ...

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

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 den vorgeschlagenen Beispielen für generische Deklarationen und vergleiche die Alternativen is und extends =. Verfolge anschließend, wie der TypeScript-Typprüfer generische Constraints und Defaults verarbeitet. Als erledigt gilt die Aufgabe, wenn eine festgelegte Syntax und Semantik für äquivalente Constraint-/Default-Deklarationen vorliegt und die Beispiele des Vorschlags durch Tests abgedeckt sind.

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

Neue Issues direkt in Ihr Postfach

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