在 Babel 7+ 中使用 TypeScript 有什么好处

What's the benefit of using TypeScript with Babel 7+

Babel 7+ 的发布引入了TypeScript support

但是使用 Babel 编译 TypeScript 比使用标准 TypeScript 编译器 (tsc) 有什么好处呢?

如果您已经在使用 TypeScript,或者从头开始一个项目,那么,根据 TypeScript and Babel 7 announcement on Microsoft Blog,最好使用 TypeScript 编译器:

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.

这意味着 Babel 7+ 支持 TypeScript 的优势主要针对那些已经在使用 Babel 并希望在不完全更改其构建系统的情况下编写 TypeScript 代码的人:

[...] 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 中 TypeScript 支持的更多信息(和说明)可在 Babel 官方网站上找到:

https://babeljs.io/docs/en/next/babel-preset-typescript.html

https://babeljs.io/docs/en/next/babel-plugin-transform-typescript.html