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.

Type info of arrow function arguments get lost when you parametrize that function

See original GitHub issue

Bug 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:open
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
cornocommented, Aug 26, 2022

@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

0reactions
cornocommented, Aug 29, 2022

On line 13, you’ve declared a generic function. But you’re actually declaring new type parameters which might come in with a different order, different count, different meaning, different anything, from the type parameter list in the contextual type. You happen to have declared a type argument list with the same names and cardinality, but this isn’t a requirement, and there’s no way to tell the difference between a type argument list that intends to correlate with the contextual type’s and one that doesn’t

This seems to be the crux of the issue. It’s a catch-22: TS won’t contextually infer a type parameter list (case 1), but explicitly specifying the type parameters (case 2) makes new, unrelated bindings.

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.

Read more comments on GitHub >

github_iconTop 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 >

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