<aside>

https://www.typescriptlang.org/docs/handbook/2/basic-types.html

https://www.youtube.com/watch?v=1UY8WBhPnlY

https://www.youtube.com/watch?v=4pqeoHLf9IU

</aside>

quick refs

practice problems

coding Qs

stack + diagram

TypeScript Qs

JS vs. TS

<aside>

TypeScript Types.pdf

TypeScript Classes.pdf

TypeScript Interfaces.pdf

TypeScript Control Flow Analysis.pdf

</aside>



Basic Types

You should always use the lowercase versions (string, boolean, symbol, bigint) for typing your variables. The uppercase versions (String, Boolean, Symbol, Number) refer to built-in JavaScript object wrappers, which behave differently and can cause subtle bugs.

let count: **number** = 0;                      // used for integer and float values
let name: **string** = "Saima";
let isDone: **boolean** = false;
let nums: **number[]** = [1, 2, 3];
let nums2: **Array<number>** = [1, 2, 3];       // equivalent generic syntax

// **Tuples** — fixed length, fixed types per position
let pair: [string, number] = ["age", 29]; 

// **Union** types — very common in interview code
let id: string | number; // id can be type string OR number

// **any** vs **unknown**: avoid `any`; `unknown` forces a type check before use
let val: unknown;
if (typeof val === "string") { val.toUpperCase(); } // OK, narrowed

Function Syntax

// Named Function with typed params and return type
function add(a: number, b: number): number {
  return a + b;
}

// Arrow Function, same thing
const add2 = (a: number, b: number): number => a + b;

// Optional and Default Params
function greet(name: string, greeting: string = "Hello"): string {
	// define greeting in function call
  return `${greeting}, ${name}`; 
}
function maybeGreet(name?: string): string {
	// define optional name in function call or use default return value
  return name ? `Hi ${name}` : "Hi stranger"; 
}

// void return (no value) vs never (function never returns, e.g. always throws)
function log(msg: string): void { console.log(msg); }
function fail(msg: string): never { throw new Error(msg); }

Arrays

the standard TypeScript (JavaScript) Array is already an ArrayList by default → natively dynamic + resizable

// number[] and Array<number> are identical
const nums = [5, 3, 8, 1];

nums.**push**(9);              // add to end
nums.**pop**();                // remove + return last
nums.**shift**();              // remove + return first
nums.**unshift**(0);           // add to start

nums.**map**(n => n * 2);                    // new array, transformed
nums.**filter**(n => n > 3);                 // new array, subset
nums.**reduce**((acc, n) => acc + n, 0);     // fold to single value
nums.**find**(n => n > 3);                   // first match or undefined
nums.**findIndex**(n => n > 3);              // index of first match or -1
nums.**some**(n => n > 3);                   // any match? boolean
nums.**every**(n => n > 0);                  // all match? boolean
nums.**includes**(3);                        // boolean membership check

nums.**sort**((a, b) => a - b);              // ascending — MUST pass comparator for numbers
// nums.sort() alone sorts lexicographically ("10" before "2") — classic bug

nums.**slice**(1, 3);           // copy, [start, end) — does NOT mutate
nums.**splice**(1, 2);          // removes 2 elements starting at index 1 — MUTATES, returns removed

[...nums].**sort**((a, b) => a - b);   // sort without mutating original

Array.**from**({ length: 5 }, (_, i) => i);   // [0,1,2,3,4]
new Array(5).**fill**(0);                     // [0,0,0,0,0]

Strings

const s = "hello world";

s.**split**(" ");            // ["hello", "world"]
s.**split**("");              // char array: ["h","e",...]
[...s];                    // also splits into chars, safer for unicode

s.**slice**(0, 5);            // "hello", [start, end)
s.**charAt**(0);               // "h"
s[0];                       // "h" — same thing, more common in practice
s.**charCodeAt**(0);          // 104 — for char-arithmetic problems
String.**fromCharCode**(104); // "h"

s.**toUpperCase**() / s.**toLowerCase**();
s.**trim**();                 // removes white space from beginning and end of string, not middle
s.**padStart**(10, "0");      // left-pad to reach given length -> ex. "5" * (3, "0") -> "005"
s.**includes**("wor");        // true or false
s.**indexOf**("wor");          // -1 if not found
s.**replace**("world", "there");   // first match only
s.**replaceAll**("o", "0");

// Strings are **immutable** — build with an array + join, not += in a loop
const chars: string[] = [];
chars.**push**("a");
chars.**join**("");

Maps + Sets

Map:

Set: