-
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 declaring properties inside class constructors #12613
Comments
@RyanCavanaugh any thoughts on this? |
You can already do something like this (slightly unsafe if the constructor is aliased by a constructor with more arguments, but this is rare): class Foo {
constructor(x: string);
constructor(public x: string,
public y = x.toUpperCase(),
private z = 3) {
}
} |
Ah, I didn't know I could access other parameters when assigning a value to constructor parameters like that. It'd still be nice to have the feature, but this is good enough for me, so I'll close the issue. Feel free to re-open if someone else thinks this would be worth introducing. |
Actually, I want to re-open this one. With es6 classes, this feature would make it much, much less painful to upgrade from JavaScript to TypeScript. There are many perfectly valid, well written javascript classes that do something like class Foo {
constructor() {
this.port = process.env.PORT || 12345;
}
} And on converting to TypeScript these will all give errors that could be avoided if this was added to the language. I just tried upgrading a medium-sized JavaScript project to TypeScript, and just over half of all errors are of this type. @RyanCavanaugh while your workaround will work ok for existing TypeScript projects, it's not very idiomatic, so it doesn't help those moving from JavaScript (plus it's confusing). Do you know how realistic it is to hope to get this feature into a future typescript release? |
It's a little clunky setting up properties in classes right now.
Here's what I have to do at the moment to set a property value in the constructor:
Notice that
interruptedSound
's setup is split over two lines, and I have to declare it as a string even though it could be inferred from the fact thatsound.substring
returns a string.What I'd like to be able to do:
This would avoid having to declare all properties at the top before being able to use them in constructors, saving a lot of boilerplate on complex classes. F# already does something similar to make setting up types much quicker and simpler. There wouldn't be any JavaScript compatibility issues, because JavaScript already works this way.
In cases where the property is declared separately, give precedence to the declaration.
The
private
,public
orreadonly
keywords could even be used before the assignment to modify the property, e.g.The text was updated successfully, but these errors were encountered: