Comment by vivzkestrel
Comment by vivzkestrel 20 hours ago
stupid question: why doesnt google chrome support native typescript execution on their chromium engine?
Comment by vivzkestrel 20 hours ago
stupid question: why doesnt google chrome support native typescript execution on their chromium engine?
I think there's also option 3, v8 understands types and uses them for optimization, but handles wrong types gracefully.
I don't think any type of understanding TS would require changing ECMAScript spec. Would a TypeScript-understanding parser not be able to handle normal ECMAScript correctly? It could switch between two modes based on the file type.
For option 1 the speed of TS development is not an issue, as Chrome would only need to include some up-to-date compiler, and the TS files could specify their TS version. But doing TS compilation in the browser would only be a small nice thing for devs, for website users it would be a downgrade, as the page load would be slower because of the compiling and the larger file sizes (JS files can already be very big these days).
It's unclear whether you could build a JIT that meaningfully benefits from typescript types.
1. Hidden classes can't be created from TS interfaces because they don't represent the full data of the underlying object
2. You don't really ever want to compile code the first time you see it, because that takes a lot of memory and extra CPU cycles. By the time code has run enough to be worth compiling, you probably have enough profile data to optimize better than you could with data from the types anyway.
3. Many of the juiciest optimizations come from types that aren't representable in TS, like integers.
4. Including all the types for all your code and deps (literally all the .d.ts) is huge, and the size increase alone might nullify any performance benefit.
1. Would still be meaningful, since it would pretty much mean we could completely ignore untyped JS code aside from legacy support, the same way we can currently pretend untyped python doesn't exist
Different projects use different versions of typescript with different configurations, and almost no one wants the client to throw type errors where the untyped code would have actually worked. I.e. the are drawbacks but no advantages. This is ongoing standardisation work to allow it to _ignore_ typescript and just run the JavaScript which will be nice but not a huge win.
It's not a stupid question, but here is the TC39 proposal to add type declarations to JS: https://github.com/tc39/proposal-type-annotations
Because TypeScript is a (legacy) mess.
There's effort towards "erasable syntax" but in practice not many really want to run anything more than basic TS in the browser, simply because you lose things like 'import _ from "lodash", which requires further setup (import maps) or is impractical in larger projects.
The real reason however is that TS is just a language, not part of any standard, and browsers are not just going to support random languages headed by corporations (Microsoft) and that are not community projects.
> you lose things like 'import _ from "lodash", which requires further setup (import maps) or is impractical in larger projects.
I think importmaps are reasonably practical even for larger projects today. I know Firefox supporting multiple importmaps in the same HTML is still a wishlist item for some to avoid more scenarios that need an importmap compile step in very large projects, but importmaps are relatively easy to automate (it's just JSON wrapped in a script tag; merging them can be just a matter of a couple Object.assign calls or object spread notation; the multiple importmaps standard that Firefox is behind on is just that, a basic Object.assign merge).
> The real reason however is that TS is just a language, not part of any standard
Right, which is why the TC-39 (committee that owns JS) proposal for "erasable syntax" for "type annotations" isn't Typescript-specific and isn't a pure superset of Typescript syntax. The node implementation of erasable syntax doesn't use the Typescript compiler and doesn't support the full Typescript syntax and is relatively close to the TC-39 proposal.
> not just going to support random languages headed by corporations (Microsoft) and that are not community projects.
Nitpicking somewhat, but Typescript has been open source since it was publicly launched and a fair enough chunk over the years of contributors aren't/haven't been Microsoft employees. It's fairly reasonable to say it is a community project. I appreciate the point that it is still seen as a specific-vendor solution and the point of separating the JS language standard proposal from being "just do what Microsoft decides is right for Typescript" (as TC-39 is trying to do), but to be fair to Typescript, it is a also bit wilder and more open than that "just a Microsoft solution" at this point (and arguably also for most of its history).
The "types as comments" proposal has a stated goal to not codify any semantics to the annotations.
This is not only due to implementation complexity, but also to keep TS to be able to change. Or even to build an entire different JS superset. With that proposal, even flow could be natively executed.
That proposal is a practical joke. You haven't got a language at all if the words don't mean anything.
It is an approach that has already seen some success in Python. Languages often have constructs that are reserved for future use or that "parse but don't validate". JS has a lot of reserved keywords (thanks to ES4 and ES5, especially) that "don't mean anything" today but could in the future and still parse even though they'll give a runtime error. Beyond that, almost every language has a syntax for comments. Comments don't mean anything to the parser/compiler either, but they mean a lot to the people writing the source files and they still need syntax to write them.
If you allow arbitrary meaningless data in today, you can never add meaning in the future. That's why the reserved words cannot be used. They need to be safe to add meaning to down the line!
I'm not enough of an expert in PL theory and compilers to say for sure, but I suspect that because Typescript's syntax and semantics are all designed with total type erasure in mind, there's no advantage you could get to executing it natively versus compiling to JavaScript and taking advantage of all the optimizations JS engines already have.
"Native typescript execution" can mean two different things:
1. Chrome/v8 takes TS code, compiles it down to JS internally, erases types, and then runs it like normal. This isn't going to be too hard to do, but also isn't going to be very meaningful. Compiling is a one-step process in any case, and plenty of tooling exists to make it seamless.
2. Chrome/v8 actually understands TS types at runtime, and throws exceptions for mismatches. This isn't going to be possible without a major rewrite of the v8 engine and the ECMAScript spec itself.
And a big challenge for both of these is that TypeScript is iterating at too fast a pace for something like Chrome to keep up. It's best to just leave versioning and compilation for the developer to manage and give end users a consistent JavaScript experience.