Skip to content

Getting error TS2339: Property does not exist on type for a valid ES6 class #6373

@mohitspawar

Description

@mohitspawar

I have the following ES6 class

class MyClass {
    constructor(){
        this.myclassvar = 'abc';
    }
}

When compiled/transpiled through TypeScript it generates error TS2339: Property 'myclassvar' does not exist on type 'MyClass'.

If the above code snippet is valid ES6 then TypeScript should not generate the error. The generated javascript it valid. It's just that the error scares the developers trying to use ES6 without typings.

The same ES6 class transpiles properly in BabelJS.

I know I can fix the error by declaring the variable.

class MyClass {
    myclassvar;
    constructor(){
        this.myclassvar = 'abc';
    }
}

Since TypeScript is a superset of Javascript, it should be able to handle valid ES6 without errors.

My compiler settings are

"compilerOptions": {
        "target": "es5",
        "module": "amd",
        "declaration": true,
        "noImplicitAny": false,
        "noResolve": true,
        "removeComments": true,
        "noLib": false,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions