-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Allow pass through of class-properties #12212
Comments
We need to move the property initialization transformation to the esnext transform. |
PRs are welcomed. |
we're working on this: https://github.com/bloomberg/TypeScript/pull/10/files |
I've observed a difference in the ESNext proposal spec for class properties and the current TypeScript class behavior. Consider this code: class A {
property = 'some string';
}
class B extends A {
property;
}
const instanceB = new B(); It seems that, with the wording of the ES proposal, 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: class Greeting {
greeting: string = 'generic greeting';
greet() { console.log(greeting); }
}
class HelloGreeting extends Greeting {
greeting: 'hello'; // Narrows type from string to "hello"
}
const greet = new HelloGreeting();
greet.greet();
// Current TS behavior: prints "generic greeting"
// ES proposal behavior: prints undefined 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?) |
@joeywatts thanks for raising this -- logged #27644 |
TypeScript Version:
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 likees2017
or evenesnext
.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
Expected emit:
Actual emit:
The text was updated successfully, but these errors were encountered: