microsoft / microsoft/TypeScript

Support generics on first class functions

Aperta
#40,149 5 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

first class function generics

Suggestion

I think first class functions are one of the most useful and important advantages that JavaScript has over other modern programming languages. While TypeScript's inference generally works pretty well on first class function arguments, it seems like changing generic arguments on a first class function (without wrapper functions) is not possible the way the generic argument syntax is parsed.

Use Cases

Any case where custom arguments don't need to be passed to a first class function, but generic type arguments do need to be passed. Currently, you have to either pass a first class function without generics or wrap a new function literal to use generics, otherwise you get Argument of type 'T' is not assignable to parameter of type 'F'.(2345), Expected X arguments, but got 0., and An argument for 'T' was not provided..

Examples

Desired syntax with type error: https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABBAhgGzQdRlAFgHgBUAaRAJQD4AKFAJwHMAuRExUSZmh5wgSkQC8FcvwDeAWABQiRLQCmUELSTsIXerykBfKbsmrYCWQqVgAggGci1Ok0TgA1mDgB3MLx6IJ044uWJbAIsWbT1UDGw8KgAiOQAPFABbAAc0OWjSeT9zKwsoWhgwegpeIA

Workaround without type errors: https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABBAhgGzQdRlAFgHgBUAaRAJQD4AKFAJwHMAuRExUSZmh5wgSkQC8FcvwDeAWABQiRLQCmUELSTsIXerykBfKbsmrYCWQqVgAggGci1Ok0TgA1mDgB3MLx6IJ044uWJbAIsWbT1UDGw8KgAiOQAPFABbAAc0OWjSdX4hX1NLfAsoWhgwehsGXl4gA

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

Riproduci gli esempi di sintassi desiderata e di workaround dai casi collegati di TypeScript Playground, quindi confronta gli errori di controllo dei tipi risultanti. Determina le modifiche necessarie al compilatore per gli argomenti generici nelle funzioni di prima classe e verifica che la forma desiderata superi il controllo dei tipi senza modificare il JavaScript emesso né il comportamento esistente.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
javascript, 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.