Sobes.tech
Junior — Middle

How is code written in TypeScript executed?

sobes.tech AI

Answer from AI

TypeScript is a superset of JavaScript with typing, which cannot be executed directly in a runtime environment (for example, in Node.js or a browser). To run TypeScript code, it must first be compiled into JavaScript.

Execution process:

  1. Transpilation (compilation): using the tsc compiler, the TypeScript source code is transformed into equivalent JavaScript.

  2. Running JavaScript code: the resulting JavaScript file is executed in a runtime environment, such as Node.js or a browser.

Example:

# Compile example.ts to example.js
tsc example.ts

# Run the compiled file
node example.js

There are also tools that allow running TypeScript directly, such as ts-node, which compiles and executes code on the fly.