Home
React
TypeScript
July 28, 2025
1 min

Table Of Contents

01
1. TypeScript catches errors at compile time
02
2. type, interface, enum
03
3. Narrowing types
04
4. Deriving types
05
5. keyof and typeof
06
6. Generics
07
7. Utility types
08
8. satisfies operator
09
9. any vs unknown vs never
10
Safe vs Unsafe in TypeScript

“TypeScript is not making your life terrible. It’s just showing you how terrible your life already is.”

1. TypeScript catches errors at compile time

What it means: TypeScript checks your code before it runs and blocks invalid usage.

function greet(user: { name: string }) {
return user.name.toUpperCase()
}
greet({ age: 25 })
// ❌ Error: Property 'name' is missing

Why it matters: Without TypeScript, this crashes at runtime. With TypeScript, it never ships.


2. type, interface, enum

type

Defines a data shape.

type User = { id: number; name: string }

interface

Like type, but extendable and mergeable.

interface User {
id: number
name: string
}

Use interface for public object APIs.


enum

Represents a fixed set of named values.

enum Status {
Loading,
Success,
Error,
}

3. Narrowing types

What it means: Restricting values to specific allowed options.

type Operator = "+" | "-" | "*" | "/"

Now "%" is rejected at compile time.


4. Deriving types

What it means: Creating types automatically from real values.

const operations = {
"+": (a: number, b: number) => a + b,
"-": (a: number, b: number) => a - b,
}
type Operator = keyof typeof operations

Why it matters: When you change operations, Operator updates automatically.


5. keyof and typeof

typeof

Gets the type of a runtime value.

type Ops = typeof operations

keyof

Extracts keys as a union.

type Operator = keyof Ops

6. Generics

What it means: Write reusable, type-safe code.

function identity<T>(value: T): T {
return value
}

TypeScript infers T automatically.


7. Utility types

Record<K, T>

What it does: Enforces exact keys and consistent value types.

type OperationFn = (a: number, b: number) => number
const operations: Record<Operator, OperationFn> = { ... }

Now you can’t miss keys or add wrong ones.


Partial<T>

What it does: Makes all fields optional.

type PartialUser = Partial<User>

Pick<T, K>

What it does: Selects specific fields.

type UserName = Pick<User, "name">

Omit<T, K>

What it does: Removes specific fields.

type UserWithoutId = Omit<User, "id">

8. satisfies operator

What it does: Checks shape without losing inference.

const operations = {
"+": (a: number, b: number) => a + b,
"-": (a: number, b: number) => a - b,
} satisfies Record<Operator, OperationFn>

Why it’s better than : Record<...>

  • ✔ Validates structure
  • ✔ Preserves exact key types
  • ✔ Prevents missing keys

9. any vs unknown vs never

any

Turns off type safety.

let v: any = 10
v.foo.bar() // allowed, unsafe

unknown

Forces validation.

let v: unknown = "hello"
if (typeof v === "string") {
v.toUpperCase() // safe
}

never

Represents impossible values.

function fail(): never {
throw new Error("Crash")
}

Also used for exhaustive checks.

Here’s the concise version:

Safe vs Unsafe in TypeScript

Safe = TypeScript can guarantee it won’t crash at runtime. Unsafe = TypeScript has to trust you.

🚨 Unsafe

  • any → disables type checking
  • as SomeType (casts) → you might lie to the compiler
  • Using unknown without checks
  • // @ts-ignore, non-null !
let v: any = 10
v.foo.bar() // allowed, unsafe

✅ Safe

  • unknown + runtime checks
  • Union types ("a" | "b")
  • keyof typeof
  • satisfies
  • Exhaustive checks with never
let v: unknown = "hi"
if (typeof v === "string") {
v.toUpperCase() // safe
}

Tags

#ReactTesting

Share

Related Posts

React Fundamentals
useDeferredValue() for Responsive Inputs
July 30, 2025
1 min
© 2026, All Rights Reserved.
Powered By

Social Media

githublinkedinyoutube