Support generics on first class functions
See original GitHub issueSearch 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.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:5 (2 by maintainers)
Top GitHub Comments
That could cause problems with the bind operator/extensions proposal.
Fixed in #47607 I think