tsc
for TypeScriptWhen making a modern JavaScript project, you might ask yourself what is the right way to convert files from TypeScript to JavaScript?
A lot of the time the answer is “it depends”, or “someone may have decided for you” depending on the project. If you are building your project with an existing framework like tsdx, Angular, NestJS or any framework mentioned in the Getting Started then this decision is handled for you.
However, a useful heuristic could be:
tsc
babel
for transpiling and tsc
for type checkingtsc
for typesThis is a common pattern for projects with existing build infrastructure which may have been ported from a JavaScript codebase to TypeScript.
This technique is a hybrid approach, using Babel’s preset-typescript to generate your JS files, and then using TypeScript to do type checking and .d.ts
file generation.
By using babel’s support for TypeScript, you get the ability to work with existing build pipelines and are more likely to have a faster JS emit time because Babel does not type check your code.
The downside to using babel is that you don’t get type checking during the transition from TS to JS. This can mean that type errors which you miss in your editor could sneak through into production code.
In addition to that, Babel cannot create .d.ts
files for your TypeScript which can make it harder to work with your project if it is a library.
To fix these issues, you would probably want to set up a command to type check your project using TSC. This likely means duplicating some of your babel config into a corresponding tsconfig.json
and ensuring these flags are enabled:
"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 }
For more information on these flags:
© 2012-2023 Microsoft
Licensed under the Apache License, Version 2.0.
https://www.typescriptlang.org/docs/handbook/babel-with-typescript.html