What is the main purpose of compiling TypeScript to JavaScript?
TypeScript code is converted into JavaScript because browsers and Node.js can only run JavaScript. The compiler removes TypeScript-specific features like types and outputs plain JavaScript.
Click to reveal answer
beginner
What file extension does TypeScript use and what does it produce after compilation?
TypeScript files use the .ts extension. After compilation, they produce JavaScript files with the .js extension.
Click to reveal answer
beginner
How does the TypeScript compiler handle type annotations during compilation?
Type annotations are used only during development for checking errors. The compiler removes all type annotations in the output JavaScript because JavaScript does not support types.
Click to reveal answer
beginner
What command is commonly used to compile a TypeScript file named app.ts?
The command is tsc app.ts. This runs the TypeScript compiler and creates a JavaScript file named app.js.
Click to reveal answer
intermediate
Can TypeScript compile to different versions of JavaScript? How?
Yes. You can set the target JavaScript version (like ES5, ES6) in the tsconfig.json file or with the --target option. This controls the JavaScript features used in the output.
Click to reveal answer
What does the TypeScript compiler remove when converting to JavaScript?
AVariable names
BAll functions
CComments only
DType annotations and interfaces
✗ Incorrect
The compiler removes type annotations and interfaces because JavaScript does not support them.
Which file extension is generated after compiling a TypeScript file?
A.ts
B.tsx
C.js
D.json
✗ Incorrect
TypeScript files (.ts) compile into JavaScript files (.js).
Which command compiles a TypeScript file named main.ts?
Anode main.ts
Btsc main.ts
Cnpm main.ts
Dcompile main.ts
✗ Incorrect
The TypeScript compiler command is tsc, so tsc main.ts compiles the file.
Why do we compile TypeScript to JavaScript?
ABecause JavaScript cannot run TypeScript directly
BTo add more types to the code
CTo make the code run faster
DTo convert JavaScript to TypeScript
✗ Incorrect
Browsers and Node.js only understand JavaScript, so TypeScript must be compiled to JavaScript to run.
How can you control the JavaScript version output by the TypeScript compiler?
AUsing the <code>--target</code> option or <code>tsconfig.json</code>
BBy renaming the output file
CBy changing the file extension to .js
DBy running JavaScript code first
✗ Incorrect
The --target option or tsconfig.json file lets you specify the JavaScript version for the output.
Explain in your own words why TypeScript code needs to be compiled to JavaScript before running in a browser.
Think about what browsers can run and what TypeScript adds.
You got /4 concepts.
Describe the role of the TypeScript compiler and what happens to type annotations during compilation.
Focus on what the compiler does with types and the output.
You got /4 concepts.
Practice
(1/5)
1. What does the TypeScript compiler (tsc) do when converting TypeScript code to JavaScript?
easy
A. It removes all type annotations and outputs plain JavaScript code.
B. It runs the TypeScript code directly without changes.
C. It adds extra type checks to the JavaScript code.
D. It converts JavaScript code back to TypeScript.
Solution
Step 1: Understand the role of the TypeScript compiler
The tsc command compiles TypeScript code by removing type annotations and producing JavaScript code.
Step 2: Identify what happens to types
Type annotations are only for development and are removed during compilation, so the output is plain JavaScript.
Final Answer:
It removes all type annotations and outputs plain JavaScript code. -> Option A
Quick Check:
TypeScript compiler removes types = C [OK]
Hint: Remember: TypeScript types vanish after compiling [OK]
Common Mistakes:
Thinking TypeScript runs directly without compiling
Believing types stay in the output JavaScript
Confusing compilation with runtime type checks
2. Which of the following is the correct command to compile a TypeScript file named app.ts into JavaScript?
easy
A. node app.ts
B. run app.ts
C. tsc app.ts
D. compile app.ts
Solution
Step 1: Identify the TypeScript compiler command
The official command to compile TypeScript files is tsc.
Step 2: Match the command with the file name
Using tsc app.ts compiles the file app.ts into JavaScript.
Final Answer:
tsc app.ts -> Option C
Quick Check:
Compile with tsc = D [OK]
Hint: Use 'tsc filename.ts' to compile TypeScript [OK]
Common Mistakes:
Trying to run TypeScript files directly with node
Using incorrect commands like 'compile' or 'run'
Confusing tsc with other tools
3. Given this TypeScript code:
function greet(name: string) {
return `Hello, ${name}!`;
}
console.log(greet("Alice"));
What will be the output after compiling and running the JavaScript?
medium
A. Hello, Alice!
B. Hello, undefined!
C. Type error at runtime
D. SyntaxError
Solution
Step 1: Understand the compiled JavaScript
The TypeScript compiler removes the type : string and outputs a normal JavaScript function.
Step 2: Predict the function output
Calling greet("Alice") returns the string Hello, Alice! which is logged to the console.