TypeScript code is turned into JavaScript so browsers and servers can understand and run it.
How TypeScript compiles to JavaScript
tsc filename.ts
tsc is the TypeScript compiler command.
You run it in the terminal to convert .ts files into .js files.
app.ts file into app.js.tsc app.ts
app.ts and keeps watching for changes to recompile automatically.tsc --watch app.ts
src/index.ts and puts the JavaScript output into the dist folder.tsc --outDir dist src/index.ts
This TypeScript code defines a text variable and prints it.
When compiled, it becomes plain JavaScript that browsers can run.
const greet: string = 'Hello, friend!'; console.log(greet);
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.
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.
Practice
tsc) do when converting TypeScript code to JavaScript?Solution
Step 1: Understand the role of the TypeScript compiler
Thetsccommand 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 AQuick Check:
TypeScript compiler removes types = C [OK]
- Thinking TypeScript runs directly without compiling
- Believing types stay in the output JavaScript
- Confusing compilation with runtime type checks
app.ts into JavaScript?Solution
Step 1: Identify the TypeScript compiler command
The official command to compile TypeScript files istsc.Step 2: Match the command with the file name
Usingtsc app.tscompiles the fileapp.tsinto JavaScript.Final Answer:
tsc app.ts -> Option CQuick Check:
Compile with tsc = D [OK]
- Trying to run TypeScript files directly with node
- Using incorrect commands like 'compile' or 'run'
- Confusing tsc with other tools
function greet(name: string) {
return `Hello, ${name}!`;
}
console.log(greet("Alice"));What will be the output after compiling and running the JavaScript?
Solution
Step 1: Understand the compiled JavaScript
The TypeScript compiler removes the type: stringand outputs a normal JavaScript function.Step 2: Predict the function output
Callinggreet("Alice")returns the stringHello, Alice!which is logged to the console.Final Answer:
Hello, Alice! -> Option AQuick Check:
Function returns greeting string = A [OK]
- Expecting runtime type errors
- Thinking types affect output strings
- Confusing compile-time errors with runtime
let count: number = 5 console.log(count)
When compiling, an error occurs. What is the most likely cause?
Solution
Step 1: Check TypeScript syntax rules
TypeScript requires semicolons or line breaks to separate statements properly.Step 2: Identify missing semicolon
The linelet count: number = 5lacks a semicolon, which can cause a compile error.Final Answer:
Missing semicolon after the variable declaration -> Option BQuick Check:
Semicolon missing = A [OK]
- Thinking number type is invalid
- Assuming variable names are restricted
- Believing console.log is disallowed
const data: Record = { a: 1, b: 2 };
console.log(data.c ?? 0);After compiling to JavaScript, what will be the output when running the code?
Solution
Step 1: Understand the nullish coalescing operator
The expressiondata.c ?? 0returnsdata.cif it exists and is not null/undefined; otherwise, it returns 0.Step 2: Check the property 'c' in the object
Sincedatahas no propertyc,data.cisundefined, so the expression returns 0.Final Answer:
0 -> Option DQuick Check:
Missing property with ?? returns default = B [OK]
- Expecting undefined instead of default value
- Thinking TypeScript types affect runtime output
- Confusing null and undefined behavior
Ready to prove it?
Pass the test and this pattern moves unknown
