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.

Inferring "this" from arrow function is {}

See original GitHub issue

TypeScript Version: Version 3.6.0-dev.20190601 Search Terms: This, Arrow, Functions, Infer, Inference, Empty, Object, Incorrect, Any

Code

type FunctionThisType<T extends (...args: any[]) => any> =
  T extends (this: infer R, ...args: any[]) => any ? R : any

let fn = () => {}
let value: FunctionThisType<typeof fn> = "wrong"

Expected behavior:

error TS2322: Type '"wrong"' is not assignable to type 'typeof globalThis'.

Found 1 error.

Actual behavior:

No errors found.

Playground Link: Playground

Related Issues: None found.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:16 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
RyanCavanaughcommented, Jun 13, 2019

Concrete example of desirable behavior:

function callWithThis<T extends Function>(fn: T, ths: ThisType<T>) {
    fn.call(ths);
}

const arrow = () => 0;
// Should be OK
callWithThis(arrow, null);
2reactions
jamiebuildscommented, Jun 2, 2019

@kitsonk Yes I understand how arrow functions work and I understand how TypeScript inference works. I understand that a this type in an arrow function doesn’t make much sense. However, the bug that I am reporting is that TypeScript does produce a type for this in an arrow function, and that type is incorrectly.

TypeScript could (and in fact does appear to in some cases) store the this context for an arrow function from its definition, and then it can later use that this type to fix this inference bug that I am reporting.

If TypeScript wants to change that type to any or something, that would also be fine, but right now I am able to produce correct JavaScript code that TypeScript reports as incorrect and this is the underlying cause.

And I don’t need you to explain JavaScript to me, I worked on Babel, TC39, and Flow, I gots it

Read more comments on GitHub >

github_iconTop Results From Across the Web

JavaScript Arrow Functions: How to debug them with name ...
In summary, it is a good practice to use function name inference to name the arrow functions if you want to debug your...
Read more >
Arrow Functions in JavaScript: Fat & Concise Syntax - SitePoint
As of ES6, variables and methods can infer the name of an anonymous function from its syntactic position, using its name property. This...
Read more >
Understanding Arrow Functions in JavaScript | DigitalOcean
Arrow functions are a new way to write anonymous function expressions in JavaScript, and are similar to lambda functions in some other ...
Read more >
5 Best Strategies to Write Arrow Functions in JavaScript
The arrow functions in JavaScript are anonymous. Use variables to hold these arrow functions to make debugging easier and productive. This aids ...
Read more >
5 Best Practices to Write Quality Arrow Functions
1. Arrow function name inference ... The arrow function in JavaScript is anonymous: the name property of the function is an empty string...
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