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

Error using non-null-assertion on a method with a this parameter (regression) #23040

Closed
whatisaphone opened this issue Mar 31, 2018 · 3 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@whatisaphone
Copy link

I hit a regression trying to upgrade to the latest version.

TypeScript Version: Tested with 2.8.1 and 2.9.0-dev.20180330

Search Terms:

  • non null assertion
  • The 'this' context of type 'void'

Code

interface Foo {
    required: ((this: Foo) => void);
    optional: ((this: Foo) => void) | null;
}

declare const foo: Foo;
foo.required();
foo.optional!();

Expected behavior:

Successful compilation

Actual behavior:

Error on the last line:

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

Playground Link:

https://www.typescriptlang.org/play/index.html#src=interface%20Foo%20%7B%0D%0A%20%20%20%20required%3A%20((this%3A%20Foo)%20%3D>%20void)%3B%0D%0A%20%20%20%20optional%3A%20((this%3A%20Foo)%20%3D>%20void)%20%7C%20null%3B%0D%0A%7D%0D%0A%0D%0Adeclare%20const%20foo%3A%20Foo%3B%0D%0Afoo.required()%3B%0D%0Afoo.optional!()%3B

Related Issues:

N/A

@Jessidhia
Copy link

By the title I thought it'd be a duplicate of #22934 but doesn't seem (directly) related 🤔

@mhegazy mhegazy added the Bug A bug in TypeScript label Apr 2, 2018
@mhegazy mhegazy added this to the TypeScript 2.9 milestone Apr 2, 2018
@sandersn
Copy link
Member

sandersn commented Apr 2, 2018

The check for the callee doesn't skip ! syntax (because it didn't exist at the time), doesn't find that the callee is foo.optional and defaults to void when it can't check it. The fix is to walk down past any !s when looking for the callee.

@sandersn
Copy link
Member

sandersn commented Apr 2, 2018

Fix is up at #23097

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

4 participants