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

This type parsing error and incorrect type inference #9744

Closed
stephanedr opened this issue Jul 14, 2016 · 5 comments
Closed

This type parsing error and incorrect type inference #9744

stephanedr opened this issue Jul 14, 2016 · 5 comments
Labels
Bug A bug in TypeScript Domain: Error Messages The issue relates to error messaging Fixed A PR has been merged for this issue Help Wanted You can do this

Comments

@stephanedr
Copy link

TypeScript Version: 2.0.0 (I suppose beta, though tsc -v just reports 2.0.0)

Code

function test(e: HTMLElement): void {
    e.onclick = (this: HTMLElement, event) => { }; // error TS1005: ')' expected.
                                                   // error TS1005: ';' expected.
                                                   // error TS1128: Declaration or statement expected.

    e.onclick = function(this: HTMLElement, event) { }; // error TS7006: Parameter 'event' implicitly has an 'any' type
}

Expected behavior:
1st: no parsing error.
2nd: event: MouseEvent inference, as for e.onclick = function(event) { };

Actual behavior:

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Domain: Error Messages The issue relates to error messaging labels Jul 14, 2016
@RyanCavanaugh
Copy link
Member

This is still going to be an error because it doesn't make sense for an arrow function to specify its this parameter (it gets the outer this), but we should give a semantic error instead of a parse error

@RyanCavanaugh
Copy link
Member

RyanCavanaugh commented Jul 14, 2016

Regarding the second thing, this is because we don't contextually type parameters when any parameter has a type annotation. There's a bug on that somewhere but I can't find it at the moment; we should revisit it (if it's closed) at the very least for the case where the only annotated parameter is the this the parameter

Edit: #4241

@mhegazy mhegazy added the Help Wanted You can do this label Jul 22, 2016
@mhegazy mhegazy added this to the Community milestone Jul 22, 2016
@mattmccutchen
Copy link
Contributor

Let me add on to this issue (it doesn't seem worth filing another): a this parameter on a generic arrow function parses but has no effect:

let f = <T>(this: number) => {  // no error
    let that = this;  // resolved the same way as if `this: number` were not specified;
                      // may be an error depending on context and compiler options
};

@mattmccutchen
Copy link
Contributor

Fix is up at #25863.

mattmccutchen added a commit to mattmccutchen/TypeScript that referenced this issue Jul 23, 2018
@mhegazy mhegazy modified the milestones: Community, TypeScript 3.1 Jul 24, 2018
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Jul 24, 2018
@mhegazy
Copy link
Contributor

mhegazy commented Jul 24, 2018

thanks @mattmccutchen!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: Error Messages The issue relates to error messaging Fixed A PR has been merged for this issue Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

4 participants