乐闻世界logo
搜索文章和话题

Whats the difference between tsc typescript compiler and ts node?

2个答案

1
2

tsc (TypeScript Compiler)

  • Definition: tsc is the official TypeScript compiler, which is part of the TypeScript language.
  • Function: It compiles TypeScript code into JavaScript code. Since TypeScript is a superset of JavaScript, it must be compiled into JavaScript to run in any JavaScript-supported environment.
  • Usage: You use tsc when generating JavaScript files for production deployment or in contexts requiring pure JavaScript code.
  • Process: Typically, the tsc compilation process includes type checking and generating corresponding JavaScript files. This may involve multiple steps, such as converting from .ts to .js, from .tsx to .jsx, or other transformations based on the tsconfig.json configuration.
  • Installation: It is usually installed as part of the TypeScript package (npm install -g typescript).

ts-node

  • Definition: ts-node is a third-party tool enabling direct execution of TypeScript code in a Node.js environment.
  • Function: It integrates the TypeScript compiler and Node.js, bypassing the compilation step to execute code directly.
  • Usage: It is useful for quickly running TypeScript code during development or for REPL (interactive interpreter) use.
  • Process: ts-node internally uses tsc to compile TypeScript into JavaScript and then executes this JavaScript directly in Node.js, typically without writing .js files to the filesystem.
  • Installation: It can be installed separately (npm install -g ts-node) and is commonly used as a development dependency.

In summary, tsc is primarily used for compiling TypeScript into JavaScript files for production deployment, while ts-node is more suited for quickly executing and testing TypeScript code during development. Both are essential tools in the TypeScript ecosystem but serve different scenarios.

2024年6月29日 12:07 回复

The most common practice is to use tsc for production builds and ts-node for development purposes, typically run alongside it. This is the command I often use for development mode in my Node/TypeScript projects: --watch with nodemon.

json
"dev": "nodemon -w *.ts -e ts -x ts-node --files -H -T ./src/index.ts"
2024年6月29日 12:07 回复

你的答案