-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
specify type of a property #1498
Comments
I think the best solution to this issue is actually the solution to #452. What do you think? |
@samwgoldman, could you be more specific, I don't see there anything that could help me here. |
@coot I think what @samwgoldman means is that you could do something like the following in the future (module syntax): function MyClass[this: { x: number }]() {
this.x = 1;
} |
Did I missed it in the docs? I think I haven't stumbled on how to specify type of this in functions. |
No, it's not implemented. The linked issue is still open. My point is that solving that issue will also solve this one. |
We recommend you use ES6 classes. Please let me know if you still have questions 👍 |
Currently this code will raise a syntax error (
Unexpected token :
):There are some workarounds:
or use ES6 classes
But when introducing flow to an existing code base, one might not want to rewrite all the code.
I tried to use
this.x = (1: number)
but this will not assure that thex
property will be always of a number type. Another possible choice is to define an interface and type use it whenMyClass
is instantiated, but then one has to remember to do that, and interfaces are structural rather than nominal, like classes are.Though it does not seem useful, it is only because the example is very simple. A real example in which one would like to specify type directly is when the property is a knockout observable which could hold only a specific type (knockout observables are functions with return a type
T
and which can set the stored value when given a parameter (of typeT
)).The text was updated successfully, but these errors were encountered: