What if your code could warn you about mistakes before you even run it?
What is TypeScript - Why It Matters
Imagine writing a big JavaScript app where you have to keep track of what kind of data each part uses, but you do it all by memory or notes.
You might accidentally mix numbers and words, causing bugs that only show up when users run your app.
Without a system to check your data types, you spend hours hunting down errors that crash your app.
It's easy to forget what kind of data a function expects or returns, leading to confusing mistakes.
TypeScript adds a helpful layer on top of JavaScript that checks your data types as you write code.
This means many errors are caught early, before your app even runs, saving time and frustration.
function add(a, b) { return a + b; } // might add numbers or join strings by mistakefunction add(a: number, b: number): number { return a + b; } // only adds numbers, errors if used wrongTypeScript lets you build bigger, safer apps by catching mistakes early and making your code easier to understand.
Think of a team building a shopping website: TypeScript helps everyone know exactly what data each part needs, so the site works smoothly without unexpected crashes.
Manual JavaScript can lead to hidden bugs from wrong data types.
TypeScript checks types while coding, preventing many errors early.
This makes your code safer, clearer, and easier to maintain.
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
