Typescript 工具链(TS 体系 - 1)

355 阅读2分钟

Typescript 工具链

Typescript的执行大部分情况下都需要进行compiler,即从一种语言转化到另一种语言。因此Ts也衍生出了很多不同的编译器。

转换和执行

最近流行的编译器

esbuild

esbuild does not do any type checking so you will still need to run tsc -noEmit in parallel with esbuild to check types.

根据文档描述可知,esbuild本身不做类型检测,只是进行类型擦除,并指明应该并行运行tsc以确保代码的质量可控.

tsx loader 支持执行TS代码.


tsx ./file.ts

swc

SWC can be used for both compilation and bundling. For compilation, it takes JavaScript / TypeScript files using modern JavaScript features and outputs valid code that is supported by all major browsers.

SWC is 20x faster than Babel on a single thread and 70x faster on four cores.

Swc can also compile typescript / tsx to ecmascript. Note that it does not type-check at the time of writing. Type checking is tracked at #126.

swc可谓是新时代的产品。基于目前流行的Rust。不过目前swc还是不支持类型检查的,关于为什么不支持,以及作者想法的变化的详细信息,可以参考#126


swc ./file.ts


tsc

tscTypeScript官方推出的编译器,捆绑在Ts包内,包含了类型检查和编译功能。

tsc ./file.ts

babel

Using the TypeScript compiler is still the preferred way to build TypeScript. While Babel can take over compiling/transpiling – doing things like erasing your types and rewriting the newest ECMAScript features to work in older runtimes – it doesn’t have type-checking built in, and still requires using TypeScript to accomplish that. So even if Babel builds successfully, you might need to check in with TypeScript to catch type errors. For that reason, we feel tsc and the tools around the compiler pipeline will still give the most integrated and consistent experience for most projects.

So if you’re already using TypeScript, maybe this doesn’t change much for you. But if you’re already using Babel, or interested in the Babel ecosystem, and you want to get the benefits of TypeScript like catching typos, error checking, and the editing experiences you might’ve seen in the likes of Visual Studio and Visual Studio Code, this is for you!

关于babel 的部分,其实还挺复杂的。babel也指出官方的tsc作为类型检查总是必不可少的一部分的时候,虽然Babel可以接管编译/转写工作.但是即使Babel构建成功.这部分代码仍是不安全的。