Bird
Raised Fist0
Typescriptprogramming~10 mins

What is TypeScript - Visual Explanation

Choose your learning style10 modes available
Concept Flow - What is TypeScript
Write JavaScript code
↓
Add TypeScript types
↓
Compile TypeScript
↓
JavaScript output
↓
Run in browser or Node.js
TypeScript is JavaScript with added types. You write code with types, then compile it to plain JavaScript to run.
Execution Sample
Typescript
function greet(name: string) {
  return `Hello, ${name}!`;
}

console.log(greet("Alice"));
This code defines a function with a typed parameter and prints a greeting.
Execution Table
StepActionEvaluationResult
1Define function greet with parameter name typed as stringTypeScript checks typeNo error
2Call greet with argument "Alice"Check argument type matches stringValid call
3Function returns string `Hello, Alice!`Template string evaluatedHello, Alice!
4console.log prints the returned stringOutput to consoleHello, Alice!
5Compilation produces JavaScript without typesTypeScript removes typesfunction greet(name) { return `Hello, ${name}!`; }
💡 Execution ends after printing greeting and compiling to JavaScript
Variable Tracker
VariableStartAfter CallFinal
nameundefined"Alice""Alice"
greet return valueundefinedundefined"Hello, Alice!"
Key Moments - 3 Insights
Why do we write types like : string in the code?
Types help catch errors before running code. See execution_table step 1 where TypeScript checks the type.
Does TypeScript run in the browser directly?
No, TypeScript compiles to JavaScript first (step 5), then the JavaScript runs in the browser or Node.js.
What happens if we pass a number instead of a string?
TypeScript will show an error during compilation (step 2), preventing wrong types from running.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result of calling greet("Alice") at step 3?
A"Hello, Bob!"
B"Hello, Alice!"
CError: wrong type
Dundefined
💡 Hint
Check the 'Result' column at step 3 in the execution_table
At which step does TypeScript remove types to produce JavaScript?
AStep 5
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Action' and 'Result' columns for step 5 in the execution_table
If we change the parameter type from string to number, what will happen?
ACode runs without errors
BOutput changes to a number
CTypeScript shows an error during compilation
DJavaScript code will include types
💡 Hint
Refer to key_moments about type errors and execution_table step 2
Concept Snapshot
TypeScript is JavaScript with types.
You add types to variables and functions.
TypeScript checks types before running.
It compiles to plain JavaScript.
JavaScript runs in browsers or Node.js.
Types help catch errors early.
Full Transcript
TypeScript is a programming language that builds on JavaScript by adding types. You write code with types, like specifying a variable must be a string. TypeScript checks these types before running the code to catch mistakes early. Then, it compiles the code into plain JavaScript, which can run in any browser or Node.js environment. For example, a function greet takes a string name and returns a greeting. When you call greet("Alice"), TypeScript ensures "Alice" is a string, then the function returns "Hello, Alice!". The types are removed during compilation, so the final JavaScript runs normally. This process helps programmers avoid bugs and write clearer code.

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

  1. Step 1: Understand TypeScript's role

    TypeScript is designed to add types to JavaScript, helping catch errors before running the code.
  2. 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.
  3. Final Answer:

    To add types to JavaScript for better error checking -> Option D
  4. 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

  1. Step 1: Recall TypeScript variable syntax

    In TypeScript, the correct syntax to declare a typed variable is: let variableName: type = value;
  2. Step 2: Check each option

    let age: number = 25; matches this syntax exactly. The other options have incorrect order or missing punctuation.
  3. Final Answer:

    let age: number = 25; -> Option A
  4. 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

  1. Step 1: Understand the function behavior

    The function greet takes a string parameter and returns a greeting using template literals to insert the name.
  2. Step 2: Evaluate the console.log output

    Calling greet("Alice") returns "Hello, Alice!" which is printed to the console.
  3. Final Answer:

    Hello, Alice! -> Option A
  4. 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

  1. Step 1: Check variable type and value

    The variable count is declared as a number but assigned a string "10".
  2. Step 2: Understand TypeScript type checking

    TypeScript does not allow assigning a string to a number type variable, causing a type mismatch error.
  3. Final Answer:

    Type mismatch: assigning string to number variable -> Option C
  4. 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

  1. Step 1: Understand union types in TypeScript

    To accept either number or string, use the union type: number | string.
  2. 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.
  3. 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.
  4. Final Answer:

    function describe(input: number | string): string { return `Input is a ${typeof input}`; } -> Option B
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard