TypeScript code is turned into JavaScript so browsers and servers can understand and run it.
0
0
How TypeScript compiles to JavaScript
Introduction
When you write TypeScript code and want to run it in a web browser.
When you build a Node.js app using TypeScript and need JavaScript output.
When you want to check your TypeScript code for errors before running it.
When you want to use modern TypeScript features but still support older JavaScript environments.
Syntax
Typescript
tsc filename.ts
tsc is the TypeScript compiler command.
You run it in the terminal to convert .ts files into .js files.
Examples
This compiles the
app.ts file into app.js.Typescript
tsc app.ts
This compiles
app.ts and keeps watching for changes to recompile automatically.Typescript
tsc --watch app.ts
This compiles
src/index.ts and puts the JavaScript output into the dist folder.Typescript
tsc --outDir dist src/index.ts
Sample Program
This TypeScript code defines a text variable and prints it.
When compiled, it becomes plain JavaScript that browsers can run.
Typescript
const greet: string = 'Hello, friend!'; console.log(greet);
OutputSuccess
Important Notes
The compiled JavaScript removes all TypeScript types because JavaScript does not use types.
You can configure the compiler with a tsconfig.json file to customize output.
Errors in TypeScript code are caught during compilation before running the JavaScript.
Summary
TypeScript code is converted to JavaScript using the tsc command.
This process removes types and creates code that browsers and servers can run.
Compiling helps catch errors early and supports using new TypeScript features safely.