Please enable JavaScript.
Coggle requires JavaScript to display documents.
TypeScript Basics - Coggle Diagram
TypeScript Basics
- Union, Literal, Intersection & Enum Types
- Union: variable can hold one of multiple types.
- Literal: variable restricted to specific values.
- Intersection: combines multiple types into one.
- Enum: defines named constants.
- Success Scenario: Assign valid types or values.
- Error Scenario: Invalid type or missing property.
- Methods/Concepts:
type ID = string | number, enum Role, type AB = A & B.
- Built-in helpers to manipulate types.
- Partial: makes all properties optional.
- Pick: selects specific properties.
- Omit: removes specific properties.
- Success Scenario: Correct property selection.
- Error Scenario: Selecting non -existent property.
- Methods/Concepts:
Partial<T>, Pick<T, K>, Omit<T, K>.
- Arrays hold lists of items.
- Tuples hold fixed-length groups with different types.
- Success Scenario: Access elements correctly.
- Error Scenario: Wrong type inside array/tuple.
- Methods/Concepts: Array syntax (
string[], Array<number>), tuple syntax ([number, string]).
- Add argument and return type annotations.
- Use arrow functions for conciseness.
- Success Scenario: Call with correct types.
- Error Scenario: Wrong argument type.
- Methods/Concepts: Function annotation (
function add(x: number, y: number): number), void return type.
- Superset of JavaScript with static typing.
- Helps catch errors at compile time.
- Compiles to plain JavaScript.
- Methods/Concepts: Compilation (
tsc), type checking, IDE type support.
- Create a project, install TypeScript, initialize
tsconfig.json.
- Success Scenario: Compiles without errors.
- Error Scenario: Invalid configuration.
- Methods/Concepts:
npm install typescript, npx tsc --init, npx tsc.
- Types:
string, number, boolean, null, undefined.
- Success Scenario: Correct type assignment.
- Error Scenario: Assigning wrong type.
- Methods/Concepts: Type annotations (
let age: number).
- Type Aliases vs Interfaces
- Describe object shapes for reuse.
- Success Scenario: Object matches type/interface.
- Error Scenario: Extra or missing properties.
- Methods/Concepts:
type User = {...}, interface Product {...}.
- Type Inference & Assertion
- TS infers types automatically; use assertion to override.
- Success Scenario: Use inferred type correctly.
- Error Scenario: Incorrect type assertion leads to runtime issues.
- Methods/Concepts:
as keyword, inferred types.
- Type Narrowing & Type Guards
- Narrow down variable types using conditions.
- Success Scenario: Safe access to properties/methods.
- Error Scenario: Accessing property of wrong type.
- Methods/Concepts:
typeof, instanceof, custom type guard functions.
- Write reusable functions/types while preserving type safety.
- Success Scenario: Works with different types.
- Error Scenario: Passing incompatible type.
- Methods/Concepts:
<T>, constraints (T extends HasLength).