-
Notifications
You must be signed in to change notification settings - Fork 67
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
Cannot perform binary '>=' operation ... #140
Comments
If the hash values are all of the same type at declaration (and in your case they are, because there is only 1), the compiler assumes that the hash will only contain values of that type in its lifetime. That assumption fails in your case. TypeScript solves this problem by letting user define an interface, a more durable yet much more verbose solution. A workaround in your case would be to add another data type to your object at declaration:
or add nothing to the hash at all and set obj.a later:
I could disable this feature if it's unpopular, but I personally kind of like the approach of locking down the hash type. In the end, I wanted to replace this system altogether with a more powerful one I built as a sub-project: https://github.com/atsepkov/Interstate. The problem is that Interstate isn't finished, and I had to work on other things instead, so I set it aside. However, even in its current state Interstate should be a lot more powerful. It already does a good job tracking all possible types for each variable (that it has seen in variable's lifetime), but I want to enhance it to narrow down current possible states. Interstate also currently does not handle properties, but I don't think it would be hard to add them. |
The other failure mode with Interstate is that it should be refactored to use promises instead of the current linear timeline approach. I did not realize this when I started writing it, but use of promises would allow it to resolve variables at a later date, even if their declaration is not yet available to reference. A proper promise chain combined with partial variable resolution would allow interstate to blow the doors off even what TypeScript can do, because TypeScript is not smart enough to detect violations through multiple function declarations (see my presentation on it: http://slides.com/atsepkov/intelligent-compilers#/, of particular interest is the slide titled "TypeScript isn't Perfect"). |
Thanks for the reply! |
I think this kind of type checking could use a command line switch to activate or deactivate them. |
The main problem of silent declarations is: you see the hash of ten pairs - nine strings values and only one number value (for ex. - width : 10 ) - is that the trick or bug (should be - width: '10px')? What did author want to say? You can't answer this question without inspecting code to understand how/where that hash is used! |
Good point, although that's what comments are for. The other use case I started considering after is use of hash as a set of configuration parameters, which this doesn't address well. In that case you would want each specific key to always be a specific type, probably what it was first defined as:
This is where I'm hoping Interstate to pick up the slack, once I finish it. Perhaps the best way to handle this is to introduce new syntax to let the user "lock" type. That's effectively what interface in TypeScript does, I just don't like the excess verbosity. |
Hi Alex!
These rows
or even
cause compilation error Cannot perform binary '>=' operation ...
The text was updated successfully, but these errors were encountered: