Skip to content

Duplicate identifier in JS class assigning to method in constructor #9880

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

Closed
RyanCavanaugh opened this issue Jul 21, 2016 · 1 comment
Closed
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@RyanCavanaugh
Copy link
Member

In a JavaScript file under --allowJs:

class Test {
  constructor() {
    this.foo = null;
  }
  foo() {}
}

Produces errors

a.js(3,5): error TS2300: Duplicate identifier 'foo'.
a.js(5,3): error TS2300: Duplicate identifier 'foo'.

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Jul 21, 2016
@mhegazy mhegazy added this to the TypeScript 2.0.1 milestone Jul 21, 2016
@sandersn
Copy link
Member

From what I remember of the discussion we had about this and the quick hack that @RyanCavanaugh wrote:

  1. There's no easy way around this because the rules are different between TypeScript and Javascript
  2. You can hack it by allowing merging of a variable and a method in Javascript only, but it's not clear what else breaks in the compiler.
  3. The right fix might be make the compiler understand merged variable/method symbols, or something else entirely (I can't remember if we came up with anything promising).

This is a legitimate-ish pattern in Javascript:

class C {
  constructor() {
    // make sure to bind all methods
    this.m = this.m.bind(this);
  }
  m() { }
}

@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Aug 17, 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
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

3 participants