TypeScript Essentials

Add types to JavaScript: primitives, objects, unions, generics, narrowing, and typing API responses, plus a strict tsconfig.

Est. 45 min Updated Suggest a fix
// Part 01 — Types and objects

Describe Values and Structured Data

TypeScript is JavaScript with a static type system. The compiler checks many mistakes before code runs, then transforms TypeScript syntax into JavaScript for the target environment. Types help editors explain available properties, help reviewers understand a function's contract, and make refactoring safer. They do not replace tests or runtime validation.

TypeScript types are erased during compilation. A declaration such as const age: number does not validate data delivered by an API at runtime. Keep that boundary in mind: types describe what your program intends to work with, while runtime checks establish whether untrusted values actually meet that description.

typescript
const learnerName: string = 'Ananya'
const completedLessons: number = 4
const isEnrolled: boolean = true

function greet(name: string): string {
  return 'Hello, ' + name
}

console.log(greet(learnerName), completedLessons, isEnrolled)
🎯 Pro Tip
Let TypeScript infer straightforward local types. Add explicit annotations where they clarify an API boundary, function contract, or public data structure instead of repeating obvious information everywhere.

The primitive types include string, number, and boolean. Arrays can be written as number[] or Array<number>. Function parameter and return types turn assumptions into checks that your editor can explain.

An object type describes its properties and their types. A type alias is a name for a type expression. Mark a property optional with ? when it can be absent, and include null in a union when the value may explicitly be null. Optional and nullable are related but not identical concepts.

typescript
type Learner = {
  id: string
  name: string
  city?: string
  mentorId: string | null
}

const learner: Learner = {
  id: 'learner-104',
  name: 'Ananya',
  mentorId: null,
}

Use an interface to describe an object shape when that style fits the project. Interfaces can be extended to express compatible shapes. Type aliases can describe objects too, and they also name unions and other type expressions. Most teams choose conventions for readability rather than claiming one construct is always better.

typescript
interface CourseSummary {
  id: string
  title: string
  lessonCount: number
}

interface FeaturedCourse extends CourseSummary {
  featured: true
}

const featured: FeaturedCourse = {
  id: 'javascript',
  title: 'JavaScript & TypeScript',
  lessonCount: 6,
  featured: true,
}
Choose syntax that communicates the data contract
Type formExampleBest for
Primitivename: stringA single value with a known basic type
Optional fieldcity?: stringA property that may be absent
Nullable unionmentorId: string | nullA property that is present but may hold null
Interfaceinterface Course { ... }An object contract that may be extended
Type aliastype Status = ...Naming object shapes, unions, and other type expressions
💡 Note
A TypeScript type is not a security or validation check on JSON received over HTTP. Check external values at runtime before using them as a CourseSummary.
// Part 02 — Unions

Model a Value That Can Have Several Valid Forms

A union type uses | to allow one of several types. For a state field, a union of string literals prevents misspelled values and documents the known states. For a network response, a discriminated union can make success and failure cases explicit to every caller.

typescript
type LoadState = 'idle' | 'loading' | 'success' | 'error'

function describeState(state: LoadState): string {
  if (state === 'loading') return 'Loading courses…'
  if (state === 'success') return 'Courses loaded.'
  if (state === 'error') return 'Could not load courses.'
  return 'Ready to load courses.'
}

A discriminated union gives each variant a shared literal property, often called a discriminant. Checking that property narrows the available fields in each branch. This is safer than a loose object with many unrelated optional properties, where combinations that should never happen are still representable.

typescript
type Result<T> =
  | { ok: true; value: T }
  | { ok: false; message: string }

function showResult(result: Result<string>): string {
  if (result.ok) {
    return result.value
  }
  return 'Error: ' + result.message
}

The check of result.ok narrows the type. In the first branch, TypeScript knows that valueexists; in the other, it knows to use message. A type that makes invalid combinations difficult to express reduces defensive code and clarifies the flow.

📌 Real World Example
A save operation might return { ok: true, savedAt: ... } or{ ok: false, message: ... }. The caller must handle both outcomes rather than assuming success.
// Part 03 — Narrowing

Prove a Value's Type Before Using It

Narrowing is TypeScript's process of refining a broad type after a check. Use typeof for primitives, equality checks for literal values, and in to check for a property. Control-flow branches tell the compiler which possibilities remain. Prefer these runtime checks over type assertions when data is uncertain.

typescript
function formatId(value: string | number): string {
  if (typeof value === 'number') {
    return 'ID-' + value.toString()
  }
  return value.trim()
}

function formatError(error: unknown): string {
  if (error instanceof Error) {
    return error.message
  }
  return 'An unexpected error occurred'
}

Catch variables are best treated as unknown: an exception can be any value, not necessarily anError. Check it before reading properties. Similarly, unknown is a safer starting point than any for values whose shape you have not established.

A type assertion such as value as CourseSummary tells the compiler to trust you. It does not convert or validate the value. Assertions are appropriate only when you have already established the fact by another reliable mechanism; otherwise they can hide the exact bug types are supposed to help find.

typescript
function getTitle(value: unknown): string {
  if (
    typeof value === 'object' &&
    value !== null &&
    'title' in value &&
    typeof value.title === 'string'
  ) {
    return value.title
  }
  return 'Untitled course'
}
⚠️ Important
Do not use as any to silence a type error as a routine fix. Understand the mismatch, validate untrusted values, and model the intended data contract accurately.
// Part 04 — Generics

Write Reusable Code That Preserves Its Type

A generic is a type parameter: a placeholder for a type supplied by the caller. Generics let reusable functions preserve the relationship between input and output types. Without them, a helper may have to returnany, losing the very information the caller needs.

typescript
function firstItem<T>(items: T[]): T | undefined {
  return items[0]
}

const firstCourse = firstItem([
  { id: 'javascript', title: 'JavaScript' },
])

// firstCourse is a course object or undefined
console.log(firstCourse?.title)

The type parameter T is inferred from the array argument. The result is the same item type, orundefined for an empty array. That return type encourages the caller to handle the empty case rather than pretending an item must exist.

Generics are especially useful for reusable containers such as a result type or a function that unwraps a value. Add constraints only when the implementation needs a capability from the type, and keep the generic API simpler than the problem it solves.

typescript
function getProperty<T, K extends keyof T>(item: T, key: K): T[K] {
  return item[key]
}

const course = { id: 'javascript', title: 'JavaScript', lessonCount: 6 }
const title = getProperty(course, 'title')
// title is inferred as string

Here, K extends keyof T restricts the key to a real property name on the input. The return typeT[K] corresponds to that selected property. This is more precise than accepting any string and returning an untyped value.

// Part 05 — Typing functions

Describe Parameters, Results, and Callbacks

Add parameter types where the expected input matters and return types where they help communicate a public contract. TypeScript can infer many return types from an implementation. For a callback, specify its parameter shape so the compiler can check both the callback and its caller.

typescript
interface Expense {
  id: string
  description: string
  amountPaise: number
}

function totalPaise(expenses: Expense[]): number {
  return expenses.reduce((sum, expense) => sum + expense.amountPaise, 0)
}

function findExpense(
  expenses: Expense[],
  matches: (expense: Expense) => boolean,
): Expense | undefined {
  return expenses.find(matches)
}

The example stores money as integer paise to avoid using a floating-point decimal as the internal amount. A real product still needs consistent rounding, currency, and server-side validation rules. Types make the chosen model visible, but they do not prove that the number is a valid amount or that an API sent it correctly.

Avoid annotating every variable if inference is already clear. However, exported functions and shared models deserve readable contracts, especially when they are used across feature boundaries. The best type is the one that captures the intended contract without obscuring it in unnecessary complexity.

🎯 Pro Tip
If an error appears far from the source, type the boundary where data enters the function. A precise input contract often makes the compiler point to the actual mismatch sooner.
// Part 06 — API boundaries

Validate JSON Before Calling It a Typed Object

response.json() returns data from the network. A cast does not inspect that data. Treat it asunknown and check the structure you need before using it. For larger schemas, teams often use a dedicated runtime validation library; for a small example, a focused type guard demonstrates the principle.

typescript
interface CourseSummary {
  id: string
  title: string
}

function isCourseSummary(value: unknown): value is CourseSummary {
  if (typeof value !== 'object' || value === null) return false
  if (!('id' in value) || !('title' in value)) return false
  return typeof value.id === 'string' && typeof value.title === 'string'
}

async function fetchCourse(url: string): Promise<CourseSummary> {
  const response = await fetch(url)
  if (!response.ok) throw new Error('Course request failed')

  const data: unknown = await response.json()
  if (!isCourseSummary(data)) {
    throw new Error('Course response has an unexpected shape')
  }
  return data
}

The return type value is CourseSummary is a type predicate. It tells TypeScript that when the function returns true, the checked value satisfies the declared shape. Keep predicates honest: every required field must be checked, and additional domain constraints may need validation too.

  1. Check the HTTP response status before treating the call as successful.
  2. Keep the parsed JSON typed as unknown at the trust boundary.
  3. Validate each field your feature relies on.
  4. Return or store the value only after validation succeeds.
  5. Handle validation failures as a visible error, not as empty success data.
⚠️ Important
Static types are not a substitute for server validation. A browser client can be modified by a user, so the server must enforce authorization and data-integrity rules independently.
// Part 07 — Strictness

Let the Compiler Find Ambiguous Assumptions

A strict TypeScript configuration enables checks that expose missing assumptions early. The exact options belong to your project, and generated templates may evolve, but starting with strict checking helps keep null and implicit type behaviour visible. Keep compiler settings consistent across development and continuous integration.

tsconfig.json (excerpt)
typescript
{
  "compilerOptions": {
    "strict": true,
    "noEmit": true
  }
}

With strict null checks, code must account for values that may be null or undefined. For example,array.find() may return undefined, so the caller should handle the not-found case. This is not unnecessary ceremony; it forces the UI to decide what happens when the expected record is absent.

typescript
const courses = [{ id: 'javascript', title: 'JavaScript' }]
const course = courses.find((item) => item.id === 'missing')

if (course) {
  console.log(course.title)
} else {
  console.log('Course was not found')
}

If a type error appears after enabling strict settings, avoid disabling checks globally as a quick fix. Identify whether the value is truly optional, whether a boundary needs validation, or whether the type is too broad. Fix the model and make the possible states explicit.

💡 Note
TypeScript catches classes of mistakes before execution, but automated tests still verify behaviour and user expectations. Use both tools for different kinds of confidence.
// Part 08 — Pitfalls

Common Mistakes

Using any for every response

Why: It disables useful checking and lets invalid properties flow through the code.

Fix: Start external data as unknown and narrow or validate it.

Assuming an assertion validates JSON

Why: A cast changes what the compiler believes but does not inspect runtime values.

Fix: Write a type guard or use a runtime schema validator at the API boundary.

Making every object field optional

Why: The type permits incomplete states the application does not actually support.

Fix: Model required fields accurately and use unions for distinct states.

Ignoring null and undefined errors

Why: The value may genuinely be absent at runtime.

Fix: Narrow the value and decide what the application should do when it is missing.

// Part 09 — Practice

Type a Course Progress Feature

Practice

Build accurate contracts from input to API result

  1. Define Course and Learner interfaces with required, optional, and nullable fields used intentionally.
  2. Create a string-literal union for loading, success, and error states, then handle each branch.
  3. Write a generic firstItem helper that safely returns undefined for an empty array.
  4. Write a type guard for an unknown course payload and use it after fetch and JSON parsing.
  5. Catch an unknown error and narrow it with instanceof Error before reading its message.
  6. Enable strict checking in a small TypeScript project and fix a missing-value error without using any.

Hint: Ask which values are trusted and which are not. TypeScript describes internal contracts; runtime checks establish external ones.

🎯 Key Takeaways

  • ✓TypeScript checks JavaScript code before runtime and emits JavaScript; annotations do not perform runtime validation.
  • ✓Use primitives, object shapes, optional properties, and nullable unions to model the data you actually have.
  • ✓Interfaces and type aliases both describe useful contracts; aliases can also represent unions.
  • ✓Discriminated unions and control-flow narrowing make valid branches explicit.
  • ✓Generics preserve the relationship between inputs and outputs in reusable helpers.
  • ✓Treat network JSON and caught errors as unknown until runtime checks establish what they are.
  • ✓Strict settings, tests, and server-side validation each provide a different layer of confidence.

Next

What comes next

Write unit tests with Vitest, structure test cases, mock modules and network calls, measure coverage and run tests in CI.

Lesson 13 · Testing JavaScript with Vitest →

HELP US IMPROVE

How was this lesson?

Your note is shared with the Chaduvuko team for review. Please don’t include personal or sensitive information.

Choose your feedback

Spotted a mistake or something unclear? Suggest a fix on GitHub or email us. Select the text first and it is quoted for you.

Share

Discussion

0

Have a better approach? Found something outdated? Share it — your knowledge helps everyone learning here.

Continue with GitHub
Loading...