📖 Scenario: You want to write TypeScript code and convert it to JavaScript so browsers can run it. To do this, you need to install and set up the TypeScript compiler on your computer.
🎯 Goal: Learn how to install the TypeScript compiler using npm, create a simple TypeScript file, compile it to JavaScript, and run the output.
📋 What You'll Learn
Install Node.js and npm on your computer
Use npm to install the TypeScript compiler globally
Create a TypeScript file named hello.ts with a simple message
Compile hello.ts to JavaScript using the TypeScript compiler
Run the compiled JavaScript file with Node.js
💡 Why This Matters
🌍 Real World
TypeScript is widely used to write safer and more maintainable JavaScript code in web and server applications.
💼 Career
Knowing how to install and set up the TypeScript compiler is essential for developers working with modern JavaScript frameworks and backend services.
Progress0 / 4 steps
1
Install TypeScript Compiler Globally
Open your terminal or command prompt and type npm install -g typescript to install the TypeScript compiler globally on your computer.
Typescript
Hint
Use npm, the Node.js package manager, to install TypeScript globally so you can use the tsc command anywhere.
2
Create a TypeScript File
Create a file named hello.ts and write this exact TypeScript code: const message: string = "Hello, TypeScript!";
console.log(message);
Typescript
Hint
Use a text editor to create hello.ts and type the exact line to declare a string variable.
3
Compile TypeScript to JavaScript
Use the TypeScript compiler by typing tsc hello.ts in your terminal to convert hello.ts into hello.js.
Typescript
Hint
Use the tsc command followed by the file name to compile your TypeScript file.
4
Run the Compiled JavaScript
Run the compiled JavaScript file by typing node hello.js in your terminal to see the output.
Typescript
Hint
Use Node.js to run the JavaScript file generated by the TypeScript compiler.
Practice
(1/5)
1. What command do you use to install the TypeScript compiler globally on your system?
easy
A. npm install typescript
B. tsc install
C. npm install -g typescript
D. install typescript -g
Solution
Step 1: Understand global installation
To use TypeScript compiler anywhere, it must be installed globally using npm with the -g flag.
Step 2: Identify correct npm command
The correct command is npm install -g typescript which installs TypeScript globally.
Final Answer:
npm install -g typescript -> Option C
Quick Check:
Global install uses npm install -g [OK]
Hint: Use npm install -g to install global packages [OK]
Common Mistakes:
Forgetting the -g flag for global install
Using 'tsc install' which is invalid
Installing locally without -g
2. Which of the following commands correctly compiles a TypeScript file named app.ts into JavaScript?
easy
A. tsc app.js
B. tsc app.ts
C. npm run app.ts
D. compile app.ts
Solution
Step 1: Identify the TypeScript compiler command
The TypeScript compiler command is tsc followed by the TypeScript file name.
Step 2: Match the correct file extension
We must compile the .ts file, so the command is tsc app.ts.
Final Answer:
tsc app.ts -> Option B
Quick Check:
Compile with tsc filename.ts [OK]
Hint: Use tsc followed by .ts filename to compile [OK]
Common Mistakes:
Trying to compile .js files with tsc
Using npm run instead of tsc
Typing wrong command like 'compile'
3. After installing TypeScript globally, what will be the output of the command tsc --version if the installed version is 5.1.3?
medium
A. TypeScript 5.1.3
B. tsc version: 5.1.3
C. 5.1.3
D. Version 5.1.3
Solution
Step 1: Understand tsc --version output format
The tsc --version command outputs 'TypeScript 5.1.3'.
Step 2: Match output to options
Only 'TypeScript 5.1.3' matches the exact output format.
Final Answer:
TypeScript 5.1.3 -> Option A
Quick Check:
tsc --version outputs 'TypeScript X.Y.Z' [OK]
Hint: tsc --version shows 'TypeScript X.Y.Z' [OK]
Common Mistakes:
Expecting only the version number without 'TypeScript ' prefix
Confusing output with npm version output
Adding extra text like 'Version'
4. You run tsc app.ts but get an error: tsc: command not found. What is the most likely cause?
medium
A. Node.js is not installed
B. app.ts file has syntax errors
C. You need to run npm start instead
D. TypeScript is not installed globally or PATH is not set
Solution
Step 1: Analyze the error message
The error 'tsc: command not found' means the system cannot find the TypeScript compiler command.
Step 2: Identify cause of missing command
This usually happens if TypeScript is not installed globally or the system PATH does not include the location of tsc.
Final Answer:
TypeScript is not installed globally or PATH is not set -> Option D
Quick Check:
Command not found means missing install or PATH [OK]
Hint: Check if tsc is installed globally and in PATH [OK]
Common Mistakes:
Assuming syntax errors cause command not found
Trying npm start which is unrelated
Thinking Node.js absence causes this error
5. You want to compile multiple TypeScript files in a project folder without typing each filename. Which setup helps you compile all files at once?
hard
A. Create a tsconfig.json file and run tsc without arguments
B. Run tsc *.ts in the terminal
C. Install TypeScript locally and run npm compile
D. Use tsc --all command
Solution
Step 1: Understand how to compile multiple files
TypeScript uses a tsconfig.json file to configure project compilation settings for multiple files.
Step 2: Use tsc with tsconfig.json
Running tsc without arguments reads tsconfig.json and compiles all files specified there.
Final Answer:
Create a tsconfig.json file and run tsc without arguments -> Option A