将 Babel 与 TypeScript 结合使用
用于 TypeScript 的 Babel 与 tsc
在制作现代 JavaScript 项目时,您可能会问自己将文件从 TypeScript 转换为 JavaScript 的正确方法是什么?
很多时候答案是 "it depends" 或 "someone may have decided for you",具体取决于项目。如果您使用现有框架(如 tsdx、Angular、NestJS 或 新手上路 中提到的任何框架)构建项目,那么此决定将为您处理。
然而,一个有用的启发式方法可能是:
- 您的构建输出与您的源输入文件基本相同吗? 使用
tsc
- 您是否需要具有多个潜在输出的构建管道? 使用
babel
进行转译,使用tsc
进行类型检查
用于转译的 Babel,用于类型的 tsc
这是具有现有构建基础设施的项目的常见模式,这些基础设施可能已从 JavaScript 代码库移植到 TypeScript。
hkVQDWgr837OzpynanNRFEVIOxLuBDFWsgMOqryu6H5bSiRPgIj+EC1Aksqai84otOIJ8rxf34Wbto7ESQ+D1H27NKwRcvDRskVY7Q7q4nuKDwTYsuxOGTs+VNVPYc7b+ZQws4S6gt6QHXqkAdf7ToVHCfbWQH36cNE2LCrLZIncF/mh4cmZtazniF+5Mn1672eJd4YKMiEQuNhoVXY0g3XBh3K5EJWHuHjdyv8cYO20yWpDYox2BJJPzywLJcGxBAmV5S31FLn0opdkaZQ9Tm45y133ItIizavpzauWccnxdf2fMVFRk+xEqYhPq6OD
m7wY2HYPeSzgSXeuXk/ZVXoOqIw5w7sp5gXYrkCaO+OP9UCsuU8Is1D8fqLrklOLrlPn/m0OSTDVRfQoPT3EXisoprsZJGtredaIxUY50YvUe0LmljQcl5Oh1U9ZjTGoORBiHRItp1RKMd3EaGd1Y9LEnw9ApxoFFRmmP+lEhSLdNQS2mQCd4TrdClq7XU933BB9TUdODqj98euSx39sn/p9ftHQkUIi4OqnUAdw6LxtUPq2dUKI9Q7mocAVBY8quzAAN1sbK7V2eNlxHyGvZw==
类型检查和 d.ts 文件生成
zUackR78IQ/SkzbfcAOZCHK5RgkqKAEX9WxdUYCr1ERPZ6MaIvG4CMkwxEEKOWKhcJm2jag2n+L3mfsLlGkGfgrh85Lfs540En7JBP7WghfaNLA1K1S8EBOOA+6GCfbjHQWxpx13Oi2epaNUvJ9RW046yMlI3W9WbrN8ADQwUpIHNZUqCJJl6vG64/Ck5PeH9KVmaDzdGh6MX1GLnutwlSy2AXAQoON0DrzMWfx+fOuhxTeShWxHJMAYo3BgIEpr
D9FU2Q87fG/m2WG6fYQcBlVFvpahrZ5tf24v9OGWR0MhTsLMv+pNWcqeqo4r8GGc/kfRLduwgXocmWrJGkU7Ka5kB/JZ0xCn/sYLaRAb+dcqNEsRoIAAoE8wZhj4KYtfdHSy7QGD51Qs1mnm4s/S0CqoL6IgTJp0GZ5VHcfTebSaC5xwQFkg3Bv1IBpStcDa
rIzdafNb7u2ew8RWKChLeGsyUWg74Zfh3tIVuRs9HvpUJ3dcuBozD9puVdXV3Xigchw6ab96WXgYkV9t4n4hANHL3gzGW5Rn6cLEizOXPECPtCLOEV79q+GTolChJfpyZyd0+8M4mScVIO2xJOguzDNL0z6nVlYfqhBwIwU8gMj2wSIEmoRqSvy+akDucmJzsAIOhCfKh6XbQjfIcOpADQ6V5KplAW2EI0EZfj20+KOQjlYaYrXyLilvBMggGNcYw6MF/nJrqvhRTodOrWZP/UQEEVMvJTlqYfIA93IxUvjP0WBPGX6H6+qb2XryMWMAb28nHRpjhWpHDEYFRkIX1f5nmvCx2uri1wA8IAOpeFrrM3SLluYe+IqeQhSf/l9k
"compilerOptions": {
// Ensure that .d.ts files are created by tsc, but not .js files
"declaration": true,
"emitDeclarationOnly": true,
// Ensure that Babel can safely transpile files in the TypeScript project
"isolatedModules": true
}
fT70Ajfvxo4ZG8T6J1d6mXyXM/N6XjUsfv5QgRd1EUJ9qWltFTw28A1AxYEwrxQA
- 09HuJYk+vH3XJFRSOyXDoDmgdH4+RRhZakT5Tod0aIv3/3HL+9p00W4hkzFLFV1GZv58n6uwrazb5Zkm/KF6ZAzbsTtyFoL++iw9PdmXnSbxVhNlIn7p9ZdHBn+ZZmWxE27L9++1vDwGz9k5vCh8yVOfwMXJ3G73aCZvHS9fYGZneIweZq3livj4wdwiUfY90wCJu02akkhLrFnWzgXFkjyxwnt0sGabrLP+37htpitBA2txfZiVE39o8lnmPKFhS13ljTwhrbPDhwkWXZoz5lMJcRHEAXOQ0o6boh6RaDeYOcUkgYPt5X7NEnLs26GMHAGtG6rO8ChcTGpff5fgUw==