Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Method with "(this: undefined)" cannot be called directly #45861

Closed
ChrisGrohHS opened this issue Sep 14, 2021 · 2 comments
Closed

Method with "(this: undefined)" cannot be called directly #45861

ChrisGrohHS opened this issue Sep 14, 2021 · 2 comments

Comments

@ChrisGrohHS
Copy link

ChrisGrohHS commented Sep 14, 2021

Bug Report

πŸ”Ž Search Terms

"this: undefined"

πŸ•— Version & Regression Information

I tried it with Nightly and 3.3.3333.

  • This is the behavior in every version I tried including Nightly, and I reviewed the FAQ for entries about "this"

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type UndefinedThisCallback = (this: undefined) => number;
const callback: UndefinedThisCallback = () => 5;
callback();// error
callback.call(undefined);// workaround

πŸ™ Actual behavior

Unable to call the method without using .call.

The 'this' context of type 'void' is not assignable to method's 'this' of type 'undefined'.

With nothing on the left side of the call, undefined should be what is passed to the callback for this.

πŸ™‚ Expected behavior

Able to call the method normally.

Why don't you use (this: void)?

When writing a library, one easy mistake is to expose internal objects via this. This can mean that changes you expected to be non-breaking can end up breaking consumers. By typing all public API functions using this: undefined, type checking gives an error to keep us from leaking internal objects.

Playground Link

type VoidThisCallback = (this: void) => number;

class MyInternalClass {
    constructor(private callback: VoidThisCallback) {
    }
    runCallback() {
        this.callback();// leaks internal object
    }
}
@MartinJohns
Copy link
Contributor

Wouldn't your issue with (this: void) be solved with #44513?

@ChrisGrohHS
Copy link
Author

Yes, it would be solved by #44513. After reading about void returning functions in the FAQ, I thought that this was also purposeful for this as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants