HarshPatel

Ahmedabad, Gujarat
Back to Blog
TypeScriptJavaScriptGenericsBest Practices

Mastering TypeScript: From Basics to Advanced Patterns

Harsh PatelMay 1, 20269 min read26 views
Mastering TypeScript: From Basics to Advanced Patterns

Why TypeScript?

TypeScript catches entire categories of bugs at compile time — null reference errors, wrong function argument types, missing object fields. It also dramatically improves IDE autocomplete, making you faster even on codebases you've never seen before.

Generics

Generics let you write reusable functions and types without losing type safety:

function first<T>(arr: T[]): T | undefined {
  return arr[0];
}

const num = first([1, 2, 3]);    // type: number | undefined
const str = first(['a', 'b']);   // type: string | undefined

Utility Types

TypeScript ships with powerful built-in utility types:

  • Partial<T> — makes all fields optional (great for PATCH endpoints)
  • Pick<T, K> — selects specific fields
  • Omit<T, K> — removes specific fields
  • Record<K, V> — creates a key-value map type
  • ReturnType<F> — infers the return type of a function

Discriminated Unions

This pattern is perfect for modeling API responses, state machines, or any "one of these shapes" scenario:

type ApiResult<T> =
  | { status: 'success'; data: T }
  | { status: 'error'; message: string };

function handleResult<T>(result: ApiResult<T>) {
  if (result.status === 'success') {
    console.log(result.data); // TypeScript knows data exists here
  } else {
    console.error(result.message); // And message exists here
  }
}

Zod for Runtime Validation

TypeScript types disappear at runtime. Use Zod to validate incoming API request bodies and get TypeScript types for free:

import { z } from 'zod';

const ContactSchema = z.object({
  name: z.string().min(2),
  email: z.string().email(),
  message: z.string().optional(),
});

type ContactInput = z.infer<typeof ContactSchema>;

Conclusion

Investing time in TypeScript pays off exponentially. Start with the basics, adopt utility types and generics gradually, and your codebase will become dramatically more maintainable over time.

All Posts
TypeScriptJavaScriptGenericsBest Practices