TypeScript: Getting error TS2339: Property does not exist on type for a valid ES6 class
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
}
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 68
- Comments: 16 (4 by maintainers)
Yes. As I mentioned the generated JS is a valid JS file. The issue here is developer experience. The error shows up in IDEs and also in the build output through GruntJS/GulpJS.
This error breaks the notion of TypeScript being a superset of Javascript and any valid JS is a valid TS.
The Typescript Specification describes Typescript as superset of ECMAScript 2015 (ES2015) which supports all ES2015 features.
Quote: “Every JavaScript program is also a TypeScript program.”
So it should definitely not error on valid JS-Code.
Link: https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#1-introduction
use
--lib es6or--lib es6,domI believe that this at least need be clarified in spec. When one reads “Every JavaScript program is also a TypeScript program” he/she can erroneously conclude that every program will run without any noticable additional effort which is not always the case.
Can it surely be a duplicate of the ticket called “Allow declaring class members within a constructor”?
It is my understanding that in ES6, we do not declare data properties of classes. The problem introduced here (and causing troubles as early as step 2 in angular2 tutorial) is that for some reason TS requires data property declaration meanwhile stating it is a superset of ES6.
Anyone managed to get Array.from to work?
http://stackoverflow.com/a/36719911/1339087
I’m still getting:
@RyanCavanaugh any chance of reopening this issue?
I agree with @mrnagydavid that this issue is not the same as #766 which proposes different methods for declaring class members within a constructor while the current one is about making a valid JavaScript code to work in a TypeScript project without generating any errors.
The idea is to solve the problem of ‘my ES6 code is an error in TypeScript’ mentioned by @danquirk here: https://github.com/Microsoft/TypeScript/issues/766#issuecomment-90204645 without introducing new syntax but instead:
This is true of syntax and runtime semantics, but it is obviously not that TypeScript cannot add semantic errors. By your argument, we could not issue an error on things like
[] * 42.This one is really annoying. Sometimes I want to have a class without cluttering it with type information.
Maybe adding something like
"noImplicitAnyForClassMembers": falseintsconfig?hi gents. I was just passing by in a search for different TS problem but I made you a small sample what should work for you. basically TS will generate you the same code that you did (is using the same JS pattern so object will have prob and behaves like class) :
http://www.typescriptlang.org/play/index.html#src=class MyClass { myclassvar%3A string %3D "abcd"%3B } var a %3D new MyClass()%3B alert(a.myclassvar)%3B
Duplicate #766