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 typing not honored #12812

Closed
wimoy opened this issue Dec 9, 2016 · 1 comment
Closed

This typing not honored #12812

wimoy opened this issue Dec 9, 2016 · 1 comment
Labels
Question An issue which isn't directly actionable in code

Comments

@wimoy
Copy link

wimoy commented Dec 9, 2016

TypeScript Version: 2.0.3

Code

interface UIElement {
    addClickListener(onclick: (this: UIElement, e: Event) => void): void;
    prop: number;
}

let uiElement: UIElement;
uiElement.addClickListener(() => {
    var k = this.prop;
});

Expected behavior:

No error

Actual behavior:
tsc.exe --noImplicitThis blah.ts
blah.ts(8,10): error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.

@jthomaschewski
Copy link

In your example you are using an arrow function - so the this available inside of this function is the same as the this outside - which has no type information (any)

Use function and you get your typed this:

interface UIElement {
    addClickListener(onclick: (this: UIElement, e: Event) => void): void;
    prop: number;
}

let uiElement: UIElement;
uiElement.addClickListener(function() {
    var k = this.prop;
});

The behavior looks good to me. It does catch errors related to misuse of arrow functions.

@wimoy wimoy closed this as completed Dec 12, 2016
@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Dec 12, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants