What is TypeScript - Complexity Analysis
We want to understand how the time it takes to run TypeScript code changes as the code or input grows.
How does the work done by TypeScript programs grow when we add more data or instructions?
Analyze the time complexity of the following code snippet.
function greet(names: string[]): void {
for (const name of names) {
console.log(`Hello, ${name}!`);
}
}
const people = ['Alice', 'Bob', 'Charlie'];
greet(people);
This code prints a greeting for each name in the list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that goes through each name in the array.
- How many times: Once for each name in the list.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 greetings printed |
| 100 | 100 greetings printed |
| 1000 | 1000 greetings printed |
Pattern observation: The work grows directly with the number of names. Double the names, double the greetings.
Time Complexity: O(n)
This means the time to run grows in a straight line with the number of items.
[X] Wrong: "The loop runs the same time no matter how many names there are."
[OK] Correct: More names mean more times the loop runs, so it takes longer.
Understanding how loops affect time helps you explain how your code handles bigger data, a key skill in programming.
"What if we changed the array to a nested array and looped inside loops? How would the time complexity change?"
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
