microsoft / microsoft/TypeScript

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

Aperta
#30,919 9 commenti 13 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia dagli esempi proposti di dichiarazioni generiche e confronta le alternative is e extends =, quindi traccia il modo in cui il verificatore di tipi di TypeScript gestisce i vincoli e i valori predefiniti generici. Il lavoro sarà completato quando saranno stabilite sintassi e semantica per le dichiarazioni equivalenti di vincolo/valore predefinito e gli esempi della proposta saranno coperti dai test.

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
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.