TypeScript helps you write safer and clearer JavaScript code by adding types. It catches mistakes before you run your program.
What is TypeScript
let message: string = "Hello, TypeScript!"; function add(a: number, b: number): number { return a + b; }
Type annotations like : string or : number tell TypeScript what type a variable or function should have.
TypeScript code needs to be converted (compiled) to JavaScript before running in browsers or Node.js.
age that must hold a number.let age: number = 25;
function greet(name: string): string { return `Hello, ${name}!`; }
let isActive: boolean = true;
This program defines a function to multiply two numbers and prints the result.
function multiply(x: number, y: number): number { return x * y; } const result = multiply(4, 5); console.log(`4 times 5 is ${result}`);
TypeScript is a superset of JavaScript, so all JavaScript code is valid TypeScript.
Using types helps you find errors early, before running your code.
You need a tool called the TypeScript compiler (tsc) to turn TypeScript into JavaScript.
TypeScript adds types to JavaScript to help catch errors early.
It improves code clarity and tooling support.
You write TypeScript, then compile it to JavaScript to run it.
Practice
Solution
Step 1: Understand TypeScript's role
TypeScript is designed to add types to JavaScript, helping catch errors before running the code.Step 2: Compare options
Only "To add types to JavaScript for better error checking" correctly describes this purpose. TypeScript does not replace JavaScript entirely with a new language, does not inherently run code faster, and is not for styling.Final Answer:
To add types to JavaScript for better error checking -> Option DQuick Check:
TypeScript adds types = B [OK]
- Thinking TypeScript replaces JavaScript
- Confusing TypeScript with CSS or styling
- Believing TypeScript runs code faster by itself
Solution
Step 1: Recall TypeScript variable syntax
In TypeScript, the correct syntax to declare a typed variable is:let variableName: type = value;Step 2: Check each option
let age: number = 25;matches this syntax exactly. The other options have incorrect order or missing punctuation.Final Answer:
let age: number = 25; -> Option AQuick Check:
Correct typed variable syntax = D [OK]
- Placing type before variable name
- Missing colon between name and type
- Incorrect order of type and value
function greet(name: string) {
return `Hello, ${name}!`;
}
console.log(greet("Alice"));Solution
Step 1: Understand the function behavior
The function greet takes a string parameter and returns a greeting using template literals to insert the name.Step 2: Evaluate the console.log output
Calling greet("Alice") returns "Hello, Alice!" which is printed to the console.Final Answer:
Hello, Alice! -> Option AQuick Check:
Template literal output = Hello, Alice! [OK]
- Confusing string interpolation syntax
- Expecting variable name printed literally
- Thinking function throws error
let count: number = "10";
Solution
Step 1: Check variable type and value
The variable count is declared as a number but assigned a string "10".Step 2: Understand TypeScript type checking
TypeScript does not allow assigning a string to a number type variable, causing a type mismatch error.Final Answer:
Type mismatch: assigning string to number variable -> Option CQuick Check:
Type mismatch error = A [OK]
- Ignoring type mismatch errors
- Thinking semicolon missing causes error here
- Assuming variable names are reserved
Solution
Step 1: Understand union types in TypeScript
To accept either number or string, use the union type:number | string.Step 2: Check return type and function body
The function returns a string describing the input type usingtypeof. function describe(input: number | string): string { return `Input is a ${typeof input}`; } correctly types input and return value.Step 3: Analyze other options
function describe(input): string { return `Input is a ${typeof input}`; } lacks input type, losing type safety. function describe(input: any): number { return typeof input; } returns number but should return string. function describe(input: number & string): string { return `Input is a ${typeof input}`; } uses intersectionnumber & stringwhich is impossible.Final Answer:
function describe(input: number | string): string { return `Input is a ${typeof input}`; } -> Option BQuick Check:
Use union types for multiple input types = A [OK]
- Using & instead of | for union types
- Omitting input type annotation
- Returning wrong type from function
Ready to prove it?
Pass the test and this pattern moves unknown
