Skip to content
This repository was archived by the owner on Jan 19, 2019. It is now read-only.

Fields with initial values in classes are reported as undefined #226

Closed
bluetech opened this issue Apr 21, 2017 · 3 comments
Closed

Fields with initial values in classes are reported as undefined #226

bluetech opened this issue Apr 21, 2017 · 3 comments

Comments

@bluetech
Copy link

What version of TypeScript are you using?

2.2.2

What version of typescript-eslint-parser are you using?

2.1.0

What code were you trying to parse?

class Foo {
    public x = 5;
}

const foo = new Foo();
console.log(foo.x);

What did you expect to happen?

x is identified as a member of Foo.

What happened?

<...>/foo.ts
  2:12  error  'x' is not defined  no-undef

Thanks (I hope this wasn't reported before).

BTW, here is the es5 output, for anyone not familiar with this syntax:

var Foo = (function () {
    function Foo() {
        this.x = 5;
    }
    return Foo;
}());
var foo = new Foo();
console.log(foo.x);
@soda0289
Copy link
Member

Thanks for the report.
Unfortunately this is a known issue. Class properties are not part of the current ecma spec which means eslint, and its scoping tool eslint-scope, do not handle these nodes correctly. We are working on ways for this parser to inform the scope analysis tool how to handle typescript nodes properly and in the future hope to support all eslint rules. Check out this issue for more details: #77

A workaround is too disable rules: no-undef and no-unused-vars. The typescript parser has two compiler options that will detect unused variable and should warn about undefined variables:

  • noUnusedLocals
  • noUnusedParameters

@bluetech
Copy link
Author

I understand. I already set these compiler options, so I followed your suggestion and disabled the eslint rules. I did the same with no-fallthrough (replaced by typescript's noFallthroughCasesInSwitch). The rule no-empty also started to fail (doesn't recognize the // empty comments), and has no typescript equivalent, so I disabled that one.

We can close this issue, unless you want to keep it open.

Thanks!

@soda0289
Copy link
Member

Comments should be detected properly starting in eslint v4. That was a long standing issue #5 that we recently fixed by having eslint handle comments.

If you find any other problems feel free to open a new issue.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants