TypeScript is a programming language that builds on JavaScript by adding types. It helps catch errors early and makes code easier to understand.
Click to reveal answer
beginner
How does TypeScript help developers?
TypeScript helps by checking types before running code, which reduces bugs and improves code quality.
Click to reveal answer
intermediate
What does it mean that TypeScript is a superset of JavaScript?
It means all JavaScript code is valid TypeScript code, but TypeScript adds extra features like types.
Click to reveal answer
beginner
What happens to TypeScript code before it runs in a browser?
TypeScript code is converted (compiled) into plain JavaScript that browsers can understand.
Click to reveal answer
beginner
Name one key feature of TypeScript.
Static typing, which means you can specify the type of variables and functions to catch mistakes early.
Click to reveal answer
What is TypeScript primarily used for?
ACreating images
BStyling web pages
CManaging databases
DAdding types to JavaScript
✗ Incorrect
TypeScript adds types to JavaScript to help catch errors and improve code quality.
Which statement is true about TypeScript?
AIt is a superset of JavaScript
BIt is a completely different language from JavaScript
CIt can run directly in browsers without conversion
DIt removes all JavaScript features
✗ Incorrect
TypeScript is a superset of JavaScript, meaning it includes all JavaScript features plus more.
Before running in a browser, TypeScript code must be:
ASaved as HTML
BConverted to CSS
CCompiled to JavaScript
DEncrypted
✗ Incorrect
TypeScript code is compiled into JavaScript so browsers can run it.
What is a benefit of using TypeScript?
AIt helps find errors before running the code
BIt makes websites load faster
CIt automatically fixes all bugs
DIt replaces HTML
✗ Incorrect
TypeScript helps find errors early by checking types before running the code.
Which feature is unique to TypeScript compared to JavaScript?
AVariables
BStatic typing
CFunctions
DLoops
✗ Incorrect
Static typing is a key feature of TypeScript that JavaScript does not have.
Explain what TypeScript is and why developers use it.
Think about how TypeScript relates to JavaScript and what benefits it brings.
You got /4 concepts.
Describe the process of how TypeScript code runs in a browser.
Remember that browsers do not understand TypeScript directly.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of TypeScript?
easy
A. To replace JavaScript entirely with a new language
B. To style web pages with colors and fonts
C. To run code faster than JavaScript
D. To add types to JavaScript for better error checking
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 D
Quick Check:
TypeScript adds types = B [OK]
Hint: Remember: TypeScript adds types to JavaScript [OK]
Common Mistakes:
Thinking TypeScript replaces JavaScript
Confusing TypeScript with CSS or styling
Believing TypeScript runs code faster by itself
2. Which of the following is the correct way to declare a typed variable in TypeScript?
easy
A. let age: number = 25;
B. let age = number 25;
C. let age: = 25 number;
D. let age number = 25;
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 A
Quick Check:
Correct typed variable syntax = D [OK]
Hint: Use colon after variable name for type in TypeScript [OK]
Common Mistakes:
Placing type before variable name
Missing colon between name and type
Incorrect order of type and value
3. What will be the output of this TypeScript code?
function greet(name: string) {
return `Hello, ${name}!`;
}
console.log(greet("Alice"));
medium
A. Hello, Alice!
B. Hello, name!
C. Error: name is not defined
D. Hello, ${name}!
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 A
Quick Check:
Template literal output = Hello, Alice! [OK]
Hint: Template literals insert variables with ${} [OK]
Common Mistakes:
Confusing string interpolation syntax
Expecting variable name printed literally
Thinking function throws error
4. Identify the error in this TypeScript code:
let count: number = "10";
medium
A. Variable name 'count' is reserved
B. Missing semicolon at the end
C. Type mismatch: assigning string to number variable
D. No error, code is correct
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 C
Quick Check:
Type mismatch error = A [OK]
Hint: Match variable type and assigned value exactly [OK]
Common Mistakes:
Ignoring type mismatch errors
Thinking semicolon missing causes error here
Assuming variable names are reserved
5. You want to write TypeScript code that only accepts a number or a string as input and returns a string describing the input type. Which code snippet correctly uses TypeScript types to do this?
hard
A. function describe(input): string {
return `Input is a ${typeof input}`;
}
B. function describe(input: number | string): string {
return `Input is a ${typeof input}`;
}
C. function describe(input: any): number {
return typeof input;
}
D. function describe(input: number & string): string {
return `Input is a ${typeof input}`;
}
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 using typeof. 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 intersection number & string which is impossible.
Final Answer:
function describe(input: number | string): string {
return `Input is a ${typeof input}`;
} -> Option B
Quick Check:
Use union types for multiple input types = A [OK]
Hint: Use | for union types to accept multiple types [OK]
Common Mistakes:
Using & instead of | for union types
Omitting input type annotation
Returning wrong type from function
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.