Inferring "this" from arrow function is {}
See original GitHub issueTypeScript 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:
- Created 4 years ago
- Comments:16 (4 by maintainers)
Top 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 >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
Concrete example of desirable behavior:
@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 forthis
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 thatthis
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