microsoft / microsoft/TypeScript

Allow omission of generic type if its equal to the default type

Aperta
#41,724 2 commenti 2 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
2g 4h
PR unite (30g)
132

Descrizione

Search Terms

generic, default, extends, equals, =

Suggestion

Allow the syntax extends = T in generics definition in place of extends T = T.

Currently

class Foo <T extends any = any> {}

Suggestion*

class Foo <T extends= any> {}

Use Cases

The problem of having to write <T extends any = any> revealed itself when I had a bit more complex type looking like keyof NetworkTransportProtocol that looked then somehow like this:

class Foo<T extends keyof system.NetworkTransportProtocol = keyof system.NetworkTransportProtocol> {}

which just was a lot of duplicated text.

class Foo<T extends= keyof system.NetworkTransportProtocol> {}

by reducing the duplicate information the whole syntax becomes better to read.

While I did focus on the default-case for class-generics. I believe this could also help with other definitions:

function foo <T extends=any>(bar: T) {}

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

La issue non indica file di implementazione né test; inizia individuando la grammatica e i test di TypeScript per i vincoli generici e i tipi predefiniti. Confronta la sintassi extends= proposta per i generics di classi e funzioni, quindi determina il comportamento richiesto del parser e del type-checking. Il lavoro sarà completato quando saranno disponibili un design accettato e una copertura di test superata per la sintassi proposta.

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.