# Practical TypeScript Patterns

> Use generics, utility types and shared types to keep a full-stack codebase type-safe without fighting the compiler. Free notes, key concepts, resources and a...

Canonical: https://www.internhack.in/roadmaps/tracks/full-stack/typescript-patterns
Last updated: 2026-11-28

Use generics, utility types and shared types to keep a full-stack codebase type-safe without fighting the compiler.

Intermediate · about 6 hours · difficulty 3/5

Learn first: [TypeScript Fundamentals](https://www.internhack.in/roadmaps/tracks/full-stack/typescript)

Once the basics feel normal, a handful of patterns cover most real-world code.

### Generics

Write a function or type once and reuse it for many shapes. A typed fetch helper like apiGet<T>(url): Promise<T> or a paginated response type Page<T> are everyday examples.

### Utility types

- **Partial** for update payloads, **Pick** and **Omit** to derive smaller shapes, **Record** for lookup tables.
- **ReturnType** and **Awaited** to reuse types from functions instead of rewriting them.
- **as const** to turn arrays of strings into literal unions.

### Sharing types across the stack

Keep request and response types in one place (a shared folder or package in a monorepo) and import them in both frontend and backend. Better still, derive them from a single source such as your Prisma schema or Zod schemas.

### Typing React

Type component props, event handlers (React.ChangeEvent<HTMLInputElement>), and state that can be null. Let inference handle useState when there is a clear initial value.

### Pitfalls

- Over-engineering with deeply nested conditional types nobody else can read.
- Duplicating the same User type in five files that drift apart.
- Exporting database models directly to the client, leaking fields like passwordHash.

You are ready to move on when you can write a generic typed API client and a set of DTO types derived with Pick and Omit from one base type.

## Key concepts

- **Generics**
- **Partial, Pick, Omit, Record**
- **ReturnType and Awaited**
- **as const and literal unions**
- **Shared types in a monorepo**
- **Typing React props and events**
- **Conditional and mapped types**

## Mini project

Build a typed API client module with a generic request function, typed errors, and DTO types for a Student resource (create, update and public view) all derived from one base type.

## Check yourself

How would you type the body of a PATCH request that may update any subset of a Student's editable fields, but never its id?

## Free resources

- [Utility Types](https://www.typescriptlang.org/docs/handbook/utility-types.html) (TypeScript)
- [Generics](https://www.typescriptlang.org/docs/handbook/2/generics.html) (TypeScript)
- [React TypeScript Cheatsheets](https://react-typescript-cheatsheet.netlify.app/) (React TypeScript Cheatsheets)

Previous: [TypeScript Fundamentals](https://www.internhack.in/roadmaps/tracks/full-stack/typescript) · Next: [Runtime Validation with Zod](https://www.internhack.in/roadmaps/tracks/full-stack/runtime-validation)

## More in TypeScript Across the Stack

- [TypeScript Fundamentals](https://www.internhack.in/roadmaps/tracks/full-stack/typescript)
- [Practical TypeScript Patterns](https://www.internhack.in/roadmaps/tracks/full-stack/typescript-patterns)
- [Runtime Validation with Zod](https://www.internhack.in/roadmaps/tracks/full-stack/runtime-validation)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Full-Stack Developer Roadmap](https://www.internhack.in/roadmaps/tracks/full-stack)
