Softechinfra
Development

TypeScript Best Practices: Type Safety & Modern Patterns

Master TypeScript with strict mode, discriminated unions, and code organization patterns that scale for production applications.

Softechinfra EditorialSoftechinfra Editorial
January 20, 20268 min read
TypeScript Best Practices: Type Safety & Modern Patterns

TypeScript has evolved significantly. What was best practice two years ago may not be optimal today. As CTO of Softechinfra, I've established TypeScript standards across all our projects. Here are current best practices.

78%
JS Projects Use TS
15%
Fewer Production Bugs
strict
Mode is Essential
0
Any Types Allowed

Type System Best Practices

1. Use Strict Mode

Enable all strict checks:

json
{
  "compilerOptions": {
    "strict": true,
    "noUncheckedIndexedAccess": true,
    "noImplicitReturns": true
  }
}

2. Prefer Type Inference

Let TypeScript work for you:

typescript
// Unnecessary
const name: string = "John"

// Better - inference works
const name = "John"

// Explicit when needed
function getUser(): User {
  // ...
}

3. Use Discriminated Unions

For complex states:

typescript
type State =
  | { status: 'loading' }
  | { status: 'success'; data: Data }
  | { status: 'error'; error: Error }

4. Avoid any

Use unknown for truly unknown types:

typescript
// Avoid
function handle(input: any) {}

// Better
function handle(input: unknown) {
  if (typeof input === 'string') {
    // TypeScript knows it's a string
  }
}

Modern Patterns

Const Assertions

Preserve literal types:

typescript
const config = {
  endpoint: '/api',
  timeout: 5000
} as const

// Type is { readonly endpoint: '/api'; readonly timeout: 5000 }

Satisfies Operator

Type checking without widening:

typescript
type Colors = Record<string, [number, number, number]>

const palette = {
  red: [255, 0, 0],
  green: [0, 255, 0],
} satisfies Colors

// palette.red is [number, number, number], not number[]

Template Literal Types

For string manipulation:

typescript
type EventName<T extends string> = `on${Capitalize<T>}`
type ClickHandler = EventName<'click'> // 'onClick'

Utility Types

Essential Utilities

typescript
// Pick specific properties
type UserPreview = Pick<User, 'id' | 'name'>

// Omit properties
type UserWithoutPassword = Omit<User, 'password'>

// Make all optional
type PartialUser = Partial<User>

// Make all required
type RequiredUser = Required<User>

// Read-only version
type ImmutableUser = Readonly<User>

Custom Utilities

Build reusable type utilities:

typescript
type NonNullableFields<T> = {
  [K in keyof T]: NonNullable<T[K]>
}

Project Organization

File Structure

  • One component/module per file
  • Co-locate types with implementation
  • Separate shared types
  • Use barrel exports carefully
  • Type Declarations

    code
    src/
      types/
        index.ts      # Shared types
        api.ts        # API types
      components/
        Button/
          Button.tsx
          Button.types.ts  # Component-specific

    Common Mistakes

    ⚠️ Avoid These: Over-typing (trust inference), type assertion abuse (use guards), any creep (address root causes), ignoring errors (fix, don't suppress), and overly complex generics.
    "Type safety isn't about adding types everywhere—it's about letting TypeScript infer where it can and being explicit where it matters."
    HB
    Hrishikesh Baidya CTO, Softechinfra

    For more on React development, see our React Server Components Guide.

    Need Type-Safe Development?

    Our development team writes clean, maintainable TypeScript code. From Next.js to Node.js, we build applications with confidence.

    Get Free Consultation

    See TypeScript in action in TalkDrill and ExamReady.

    Tags:
    TypeScriptJavaScriptBest PracticesDevelopmentReact
    Share this post:
    Softechinfra Editorial

    Softechinfra Editorial

    Articles published by Softechinfra without a personal byline.

    How we pick topics, use AI, check claims and correct mistakes: our editorial policy.