hax / hax/proposal-function-this
A solution by TypeScript of this this problem
Open
- Dominant language
- JavaScript
- Stars
- 4
- Forks
- 1
- PR merge metrics
- No merged PRs in 30d
Description
```ts
class X {
f = 1
fn(this: X) {
// ~~~~~~~ have to write this
console.log(this.f)
}
}
const x = new X
setTimeout(x.fn, 2000) // No type error but runtime bug
safeTimeout(x.fn, 2000)
// Argument of type '(this: X) => void' is not assignable to parameter of type '(this: void) => {}'.
// The 'this' types of each signature are incompatible.
// Type 'void' is not assignable to type 'X'.(2345)
function safeTimeout(
f: (this: void, ...args: T) => {}, time: number, ...args: T
) {
return setTimeout(f, time, ...args)
}
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.