Using tsc to Compile and Run
What you'll learn: You'll learn the two-step workflow of compiling TypeScript files with tsc and executing the resulting JavaScript.
The Compilation-Execution Dance
Remember that TypeScript doesn't run directly—you need to transform it into JavaScript first. The tsc command (TypeScript Compiler) does this for you. Think of it like translating a recipe written in French into English before you can cook from it.
The Basic Workflow
Here's the pattern you'll use constantly:
- Compile: Run
tsc yourfile.tsin your terminal - Execute: Run the generated JavaScript with
node yourfile.js(for Node.js) or open it in a browser
When you run tsc yourfile.ts, TypeScript checks your code for type errors and produces yourfile.js in the same directory. If there are type errors, you'll see them in your terminal, but the JavaScript file is often still created (TypeScript tries to help you even when your types are wrong).
Example Flow
Say you have greeting.ts. You'd run:
tsc greeting.ts
This creates greeting.js. Then execute it:
node greeting.js
For browser code, you'd include greeting.js in an HTML file's <script> tag instead.
Why Two Steps?
This separation might feel tedious, but it's powerful: you catch type errors before running your code, and the output JavaScript runs in any environment that supports the target version you configured.
Key Takeaway: TypeScript compilation (tsc file.ts) and execution (node file.js or browser) are always separate steps—compile first to transform TypeScript into runnable JavaScript, then execute the result.