question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support generics on first class functions

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
xiBreadcommented, Jul 28, 2021

Any love for the turbofish? returnAs::<string>

That could cause problems with the bind operator/extensions proposal.

0reactions
jcalzcommented, Apr 10, 2022

Fixed in #47607 I think

Read more comments on GitHub >

github_iconTop Results From Across the Web

First class generic function in swift?
So, the called function in your example, callFunction , still isn't getting the generic function. It's getting a specific instance of it, determined...
Read more >
First-class Functions in Chapel
Rationale. Generic functions would require manipulating generic, uninstantiated types, which is currently not available in Chapel. Functions with compile-time ...
Read more >
5. First-Class Functions - Learning Scala [Book]
Scala has full support for first-class functions, higher-order functions, ... However, wouldn't it be more useful if it was generic and supported type ......
Read more >
Generic Methods - Java™ Tutorials
Generic methods allow type parameters to be used to express dependencies among the types of one or more arguments to a method and/or...
Read more >
Java Generics Example Tutorial - Generic Method, Class ...
Bounded type parameters can be used with methods as well as classes and interfaces. Java Generics supports multiple bounds also, i.e <T extends ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found