Type info of arrow function arguments get lost when you parametrize that function
See original GitHub issueBug Report
🔎 Search Terms
arrow function type inference type parameters
see also issue: https://github.com/microsoft/TypeScript/issues/50417
🕗 Version & Regression Information
the furthest back the playground allows is 3.3.3; in that version this was already an issue
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about bugs that are not bugs
⏯ Playground Link
Playground link with relevant code
💻 Code
type MyFuncType = (a: string) => void;
const myFuncImp: MyFuncType = (a) => { // <-- the fact that this argument is of type string does not have to be respecified, just the argument name is enough
a.endsWith("foo")
}
//now trying the same with type parameters:
type MyFuncTypeWithTypeParameter = <T>(a: string) => void;
const myFuncImpWithTypeParameter: MyFuncType = <T, >(a) => { // <-- now suddenly the type of a is unknown
a.endsWith("foo")
}
🙁 Actual behavior
the type of a is not known
🙂 Expected behavior
the type of ‘a’ is known by the compiler
Issue Analytics
- State:
- Created a year ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Arrow function expressions - JavaScript - MDN Web Docs
Arrow functions don't have their own bindings to this , arguments , or super , and should not be used as methods. ·...
Read more >How to Accept Any Number of Arguments in a JavaScript ...
Arrow functions do, however, allow you to accept any number of arguments. Instead of an 'arguments' object, we can refer to a 'rest'...
Read more >What is the syntax for Typescript arrow functions with generics?
Workaround: Use extends on the generic parameter to hint the compiler that it's a generic, this came from a simpler example that helped...
Read more >JavaScript Arrow Functions with Examples - Dot Net Tutorials
If an Arrow function takes no parameter or Arrow function with no parameters, parentheses () will be empty but they should be present....
Read more >Anomalies in JavaScript arrow functions - LogRocket Blog
Arrow functions do not have an arguments binding. However, they have access to the arguments object of the closest non-arrow parent function.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@RyanCavanaugh I’ve tried to come up with a minimal example of when this might occur. I’m using ‘map’ as an example. I have lots of occurences of these kind of things. Can you explain what you feel is the issue with this code, why it should never been written in the first place?
playground
If I understand you correctly, this is indeed the problem I’m facing.
Here is an example that is closer to my actual code base. playground
I’m writing an inner function because there is a recursive structure that needs to be visited. I want the inner function to use the outer function’s parameter ‘report’ and I don’t want to copy that parameter to the inner function. This means that I have to be able to refer to the generic type ‘T’. If there is something intrinsically wrong with this approach, I would really like to hear that.