Allow pass through of class-properties
See original GitHub issueTypeScript Version:
- 2.2.0-dev.20161113 (Not version specific)
- target = esnext, jsx = preserve
Description
I expect Typescript to more or less only strip types but preserve the rest - based on the target
compilerOption. Especially when using a target like es2017
or even esnext
.
Class-properties are always moved into the constructor. This prevents hot-reloading of class-property functions when using react-hot-loader 3.
Using them is a common pattern for binding event-handler functions to this
without having to do this for every method in the constructor.
Code
class MyClass {
prop1: number = 123;
handleClick = () => {
console.log('Click handled');
}
render() {
return <button onClick={this.handleClick}>Click me</button>;
}
}
Expected emit:
class MyClass {
prop1 = 123;
handleClick = () => {
console.log('Click handled');
}
render() {
return <button onClick={this.handleClick}>Click me</button>;
}
}
Actual emit:
class MyClass {
constructor() {
super(...arguments);
this.prop1 = 123;
this.handleClick = () => {
console.log('Click handled');
};
}
render() {
return <button onClick={this.handleClick}>Click me</button>;
}
}
Issue Analytics
- State:
- Created 7 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
Support for the experimental syntax 'classProperties' isn't ...
First install the: @babel/plugin-proposal-class-properties as dev ... .babelrc file located in the root directory, where package.json is.
Read more >Managing annotation feature class properties—ArcMap
The Annotation Classes tab allows you to manage annotation classes. You can add and remove annotation classes from the list. To modify or...
Read more >Using Properties - C# Programming Guide - Microsoft Learn
Therefore, you can't pass a property as a ref or out parameter. Properties have many uses: they can validate data before allowing a...
Read more >Property Syntax - MATLAB & Simulink - MathWorks
Define class properties using attributes and validators that control behavior. ... property validation allows a subclass of the specified class to pass ......
Read more >Babel's Transform Class Properties Plugin: How it Works and ...
In this post, we'll go over a particularly useful Babel plugin for ... In JavaScript, current class syntax only allows for you to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’ve observed a difference in the ESNext proposal spec for class properties and the current TypeScript class behavior. Consider this code:
It seems that, with the wording of the ES proposal,
property
gets redefined in B, so thatinstanceB.property === void 0
. The babel implementation seems to callObject.defineProperty
in the case where a modification must be made to an existing property descriptor.It’s certainly possible to make TypeScript do the same thing, but this change has significant risk of breaking existing code. This change would likely be observed by many users since TypeScript property declarations are not only used for runtime effect - but also to add type information. (Therefore, it’s likely that many are actually using property declarations with no initializer.) See the following example:
If TS is to stay consistent with ES, then a breaking version change will need to be made (with a flag to opt-in to the existing behavior?)
we’re working on this: https://github.com/bloomberg/TypeScript/pull/10/files