现代浏览器中对 TypeScript 的支持

我刚刚阅读了一些与 TypeScript 相关的广告信息,除此之外,我没有使用这个 JavaScript 超集的经验。所以我有一些基本的问题:

  1. 现代浏览器对原生 TypeScript 的支持情况如何?

  2. 主要浏览器供应商对于未来 TypeScript 本地化支持的立场是什么?

  3. TypeScript 和 ECMAScript 6之间的关系是什么? TypeScript 比 ECMAScript 6提供了更多的特性吗?

47102 次浏览

Native support

Native support can be added quite easily e.g. https://github.com/basarat/typescript-script adds support for script tags (although this adds 6 MB of supporting JavaScript to a page). However there is a performance hit in compiling TypeScript to JavaScript and for the best performance it is best to precompile and execute the compiled JavaScript in the browsers.

Browser vendors have no plans to add native TypeScript support. The only native languages common across browsers are JavaScript and WebAssembly. TypeScript can be transpiled to either and run in the browsers so its not a blocker.

Not-native support

What is the status of TypeScript support by the modern browsers?

TypeScript compiles to JavaScript, so it's supported by all browsers (even Internet Explorer 6).

What are the positions of the major browser vendors for the future support of TypeScript?

No work is needed on behalf of browser vendors.

What is the relation between TypeScript and ECMAScript6 and does TypeScript provide much more features than ECMAScript6 ?

Yes, e.g., from future JavaScript versions, e.g., class fields and static properties in classes.

TypeScript cannot be run or understood in any browser. So, TypeScript is compiled to JavaScript (which browsers can understand). TypeScript can use all ECMAScript 2015 features and during the compilation they will be converted to target compile options like ES5.

Currently it won't work, but it's an interesting topic though. It would be good to know if at some point ECMAScript is going to support interfaces, types and other things. Especially, I can notice that ECMAScript actually slowly takes some parts from the TypeScript, e.g. enums.

If it would work, I'd rather see it as ECMAScript taking some stuff from TypeScript rather than natively supporting TypeScript itself.

Trying to design a web browser that could natively support more than one language at a time is exactly what Google tried to do with Dart. There was a Dart engine planned for Chrome, but this ended up being too big a task with very little benefit aside from "just not using JavaScript".

I suspect supporting TypeScript natively would be an equally daunting task. Probably better to just improve upon ECMAScript and add in a few TypeScript ideas where they make sense. I do love TypeScript though.

I know it's currently not supported and there are no plans to support TypeScript natively in the browser but there is a huge (in my opinion) problem with tooling in the frontend (JavaScript) ecosystem - there's too much of it. Bundling, module resolution, transpiling, and more.

I think it makes development much slower, because there are a lot of issues with configurations, versions mismatch, compatibility and a lot of obscure issues (Stack Overflow and GitHub are full of such topics) that stem from the tooling, not implementing features.

I would call it a crisis, but it has been like this for a while and I know this is the way of doing things now.

I think there are a few things that need to happen.

  1. We need to be able to use native ES6 modules (import/export) in the browser. I know it's supported, but only a relative or URL imports work. I know there is work on "import maps" or whatever they are called.

  2. It would be great if browsers (Chrome) can read TypeScript code. It doesn’t have to interpret or enforce any typing - essentially treat it like JavaScript with some additional nonsense that the browser just ignores. It could be just a "developer mode" in the browser but we would be able to achieve 0-transpiling during development.

  3. Simplify eager resource loading (to avoid bundling)

Again, this is just for development benefit. I know a lot of these tools have their place in production builds, older browser support, etc.