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

Property without declaration: es6 alignment in class constructor #6565

Closed
Alphapage opened this issue Jan 22, 2016 · 1 comment
Closed

Property without declaration: es6 alignment in class constructor #6565

Alphapage opened this issue Jan 22, 2016 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@Alphapage
Copy link

Hello,
When I try this code from #4955:

class Alpha {
  constructor() {
    this.size = 42; //error : but it should be public of type number because no declaration
  }
}
var x = new Alpha();
var y = x.size; // y: number //error

I get the error: property 'size' does not exist on type 'Alpha'.
And when using js with tsserver (vscode), then size is of type any. But in the post, it seems to be supported.
Do you have the same problem in vscode for js ? Is this feature not implemented yet ?

Thanks in advance for your help.

@Alphapage Alphapage changed the title Property without declaration: javascript alignment Property without declaration: es6 alignment in constructor class Jan 22, 2016
@Alphapage Alphapage changed the title Property without declaration: es6 alignment in constructor class Property without declaration: es6 alignment in class constructor Jan 22, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Jan 22, 2016

This is not supported in TypeScript. All property declarations need to be declared before used in the constructor. see more details about a similar proposal that was declined in #766. The rational is that property declarations provide clear declaration of intent to the compiler, and the compiler can then provide more help. implicitly inferring declarations from constructors will miss typos in assignment to constructor properties, can not be generalized to all methods, and accessors, and will lead to inconsistent class declarations.

Issue #4955, is for .js files and not .ts files. Since a JS (ES2015/ES6) class does not have a concept of property declaration (yet), the compiler needs to infere from the JS class body. this only works in JS files when passing the --allowJS. see #4792 for more information.

@mhegazy mhegazy closed this as completed Jan 22, 2016
@mhegazy mhegazy added the Duplicate An existing issue was already created label Jan 22, 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
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants