📖 Scenario: You are learning how TypeScript code is turned into JavaScript code that browsers can run. This is important because browsers do not understand TypeScript directly.Imagine you write a recipe in your own special language (TypeScript), but your friend only understands English (JavaScript). You need to translate your recipe so your friend can cook it.
🎯 Goal: Build a simple TypeScript program and see how it compiles into JavaScript code.You will write TypeScript code, then add a configuration to control compilation, then compile it, and finally see the JavaScript output.
📋 What You'll Learn
Create a TypeScript variable with a type annotation
Add a TypeScript compiler option variable
Write a function using TypeScript syntax
Show the compiled JavaScript output
💡 Why This Matters
🌍 Real World
TypeScript is used to write safer and clearer code for web apps. It compiles to JavaScript so browsers can run it.
💼 Career
Many companies use TypeScript for frontend and backend development. Understanding compilation helps in debugging and optimizing code.
Progress0 / 4 steps
1
Create a TypeScript variable with a type
Create a variable called greeting with type string and set it to "Hello, TypeScript!"
Typescript
Hint
Use let keyword, then variable name greeting, then : string to specify the type, then = and the string value.
2
Add a TypeScript compiler option variable
Create a constant called compilerOptions and set it to an object with the property target set to "ES6"
Typescript
Hint
Use const keyword, name compilerOptions, and assign an object with target: "ES6".
3
Write a TypeScript function using type annotations
Write a function called sayHello that takes a parameter name of type string and returns a string greeting using greeting and name
Typescript
Hint
Use function keyword, specify parameter type name: string, return type : string, and use template string to combine greeting and name.
4
Print the result of the function call
Write a console.log statement to print the result of calling sayHello with the argument "World"
Typescript
Hint
Use console.log(sayHello("World")) to show the greeting message.
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.