-
Notifications
You must be signed in to change notification settings - Fork 49
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
What if someday we want runtime enforced type checking? #45
Comments
This is a good point. Once you open the floodgates of allowing such syntax, it's entirely possible that there may be interest in semantics similar to PHP, for example. This sort of interest already exists today, e.g. ts-runtime. |
In the future, we may want an "official" type system that will also have runtime checks. If you look at the history of types in Python, they did a two step approach: first they enabled "types as comments" like we do here. Then, when the ecosystem experimented with it enough, they added an official type system (caveat: I haven't followed Python closely enough, so I may be way off in how this happened). |
The key difference with Python is that they can make breaking changes (infamously so). If my website has static types as comments, you can't just suddenly turn type checking on at runtime without potentially taking my site down. |
Better performance seems like the obvious potential benefit of having types, this proposal doesn't seem to have taken performance into consideration much, nor it seems to suggest that these type annotations may lead to better performance in the future. IMO if better performance is one of the major goals that are supposed to be arising from this in the future it's better not to overcommit to something potentially unsuitable that we can't fix in the future for backwards compatibility reasons. A "just spec TS" approach may lead us the wrong way. |
People should be sure to clearly distinguish between static typechecking (presumably as a pre-step to script evaluation) vs dynamic typechecking (raising an error/warning if a runtime call to |
That's a good point. Those seem maybe some of the most important problems that one would expect types to tackle. It's interesting because the current proposal doesn't seem to tackle either of those, nor it seems to provide an even vague path toward tackling either of those, nor it seems to tackle any other valuable problem, at least as far as I can see 🤔 |
That's what I'm thinking, too. The only idea: Then, the problem of breaking the web disappears, as does the problem of performance degradation - it would only work in the developer's browser. But as far as I understand, it's not only about compilation time? It's also about being able to upload this "typed" code to the web without compiling at all? |
@jamiebuilds it's still possible to introduce breaking changes in JavaScript with a flag like |
I for one would like to see ignoring type-comments enabled only in the browser's DevTools. So not any "use strict" or anything like that. Just the browser's DevTools function. |
@ktmud in practice, it is not, as the prevailing committee attitude remains “no new modes, ever”. |
So it seems that, if this continues to be the attitude of the committee, then this proposal would prevent any future possibility of runtime-enforcement of these annotations, as there's nothing stopping people from writing code with broken/inaccurate type annotations, so adding any runtime-enforcement would be backwards-incompatible |
@chrisdothtml I think that's the whole point of this issue. Implementing this in a way that would reserve the |
To be clear, I don't know that we'd ever do any kind of runtime type checking. But I think it's important to note what this proposal would lock us out of doing and have the committee agree to that. |
"It would lock us out of runtime checks using the syntax defined here, without explicit indication to the runtime engine to do the check." And if we don't actually have a supermajority on needing runtime type checking (even in the near future), this seems like the right approach. |
@wparad What are you quoting? |
I was putting concretely what we would be doing implicitly with this proposal to make it easy for others to understand what the implications might be and what could be conveyed as part of the proposal as a caveat. Stating it was my attempt to make sure that I understood the concern correctly. |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, I must have misinterpreted your comment. What seems like the right approach? |
Never say "ever" 😀 |
@glen-84, as @jamiebuilds suggested:
|
@giltayar i was careful to say "prevailing" :-p |
I can see an API like |
I went over the discussion I've had (for this proposal) with the designers of Python's type system annotations and I recalled why this is impossible anyway. There are many types that simply cannot be verified in runtime. This is true for many generic types. Some examples we all know and love are:
Basically any container that is not random access (not that iterating a 10000 item array to know it's all strings makes a ton of sense to me anyway). To me: that's a lot more motivation for why we shouldn't (ever) do this given the language than "no other language is doing this" or "this isn't popular". (Note the language does perform type checks in several places - e.g. |
@benjamingr I'm not sure I understand this? There's no reason the semantics for (for the sake of argument) |
This would mean one can hold an
Note everything in JavaScript already has a full runtime type. JavaScript already has types though they are not generic (that is, something can be an object with a generator prototype) so while something can be a
That would require "full" typing and effectively a complete breaking change for the language.
I'm not sure I can see a semantics for this where this works for stuff like promises (you hijack the error and reject the promise if you get the wrong type?) - but mostly it doesn't compose or work for user types. It would also be prohibitively slow from a runtime's point of view. |
Why? There's potential warts, but nothing blocking from what I can see. At worst, you have to add new, generic aware APIs, like in .NET 1 to 2. At best, as an example
Sure. The point is it's always legal to throw or kill the runtime, and it's always typesafe to do so instead of returning the wrong type. Handing invalid arguments to callbacks is not as easy to generically handle, but Promise is probably magical enough to get special treatment. I could imagine a
I'm not a big fan of type checking of values for several reasons, performance being a big one, yes. But it's essentially the same as checking every value of an array? |
I think that we should avoid going into a full discussion about runtime checking (which could even be limited to primitive types initially), and rather concentrate on the main point, which is: Could there be any other more appropriate uses for this syntax in future (regardless of basic type checking, advanced type checking, or even something completely different), than just being used as what is essentially another comment syntax. Does anyone believe that there will be zero better use of the If not, then there are currently 2 alternatives:
|
Yes, I believe reserving this syntax for userland type-checkers is the best use for this syntax for multiple reasons outlined in the README of this repo. I believe that if any runtime type system or checks would have been specced 10 or 20 years ago we would have been at a much worse place today in terms of being able to write expressive type systems. I also believe that moving the static type system away from the language to tooling is a good thing which is also validated by other languages who have done this. This isn't just true for Python. It's doubly true for JavaScript where performing meaningful type checks at runtime is very expensive so the type checking would have to be separate from running code anyway. |
Future type-checking in JS doesn't have come close to TS in complexity, it could be limited to basic types, as shown here, to potentially improve performance, instead of the opposite. TS/Flow would still exist, for more complex checking, and could down-level only the simple/supported types. Reserving this syntax would be a mistake IMO. |
I think this question is sufficiently answered by the README now:
I think it's okay to close this now. |
A a strawman for the reserved syntax, |
I'm super strongly in favor of this step forward with types being ignored at runtime. However, I'm concerned what this might lock us out of in the future.
There has been a lot of interest in the past in performing runtime checks of types. Google, Mozilla, and others have experimented with several different approaches that I'm aware of (i.e. "use strong"). And it's possible that we may we want to revisit that in the future. It would be a real shame (please imagine the crime boss voice) if we couldn't do that because there's all this syntax that was written to do nothing at runtime.
One possible solution to that is a new directive:
Of note, in a future where we get static checking at runtime, it wouldn't be a simple matter of removing this directive to turn it on because there's no guarantees the syntax matches. Its only purpose (that I can see) would be to reserve the syntax space for future use.
The text was updated successfully, but these errors were encountered: