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

can’t use 'this' in object literals with --noImplicitThis #10835

Closed
msklvsk opened this issue Sep 10, 2016 · 3 comments
Closed

can’t use 'this' in object literals with --noImplicitThis #10835

msklvsk opened this issue Sep 10, 2016 · 3 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@msklvsk
Copy link

msklvsk commented Sep 10, 2016

TypeScript Version: 2.1.0-dev.20160910

Code

let literal = {
  a: 1,
  getA() {
    return this.a
   // error: 'this' implicitly has type 'any' because it does not have a type annotation
  },
}

Expected behavior: there is a way to use this in an object literal while noImplicitThis enabled

Actual behavior: an error is shown, even with getA: function(this: this) notation

@yortus
Copy link
Contributor

yortus commented Sep 10, 2016

This was tried before (see PR #8382) and then reverted after the discussion in #8191.

@msklvsk
Copy link
Author

msklvsk commented Sep 10, 2016

Thanks. I gave up searching duplicates on page 2 of 50.
Then maybe we make this issue about error

this' implicitly has type 'any' because it does not have a type annotation

when --noImplicitThis enabled?

@msklvsk msklvsk changed the title this is :any in object literal can’t use 'this' in object literals with --noImplicitThis Sep 10, 2016
@kitsonk
Copy link
Contributor

kitsonk commented Sep 10, 2016

Well, if you have noImplicitThis enabled, you have opted into ensuring that this is typed, even in object literals. This works around the error:

let literal = {
  a: 1,
  getA(this: any) {
    return this.a
  },
}

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

4 participants