3 ms·
Browsers certainly collect runtime types of variables in hot code paths, but that's pretty different from the whole-program static verification that TypeScript
by alangpierce 8y ago
Browsers certainly collect runtime types of variables in hot code paths, but that's pretty different from the whole-program static verification that TypeScript does. TypeScript is designed to have an expressive and flexible type system, and typechecking a large codebase might take tens of seconds or minutes, not something you want running every time any user loads your site.
That said, including types in JS could be useful as a hint for the optimizer, but at least in TypeScript and Flow, the types can always be wrong even if no errors are reported (e.g. from misuse of escape hatches), so the runtime would need to double-check them.
- yazaddaruvala 8y agoI agree with you. They definitely should keep doing "type checking" lazily. From what I understand, it would be a lot cheaper if types were built into the language, and the runtime's contract could trust the type and if it is wrong then undefined behavior occurs.