Skip to content

Files

Latest commit

 

History

History
83 lines (68 loc) · 1.54 KB

no-namespace.md

File metadata and controls

83 lines (68 loc) · 1.54 KB

Pattern: TypeScript namespace usage

Issue: -

Description

TypeScript namespaces (formerly called "internal modules") are an outdated way to organize code. Modern TypeScript code should use ES2015 modules with import/export statements for better tooling support, tree shaking, and compatibility with the JavaScript ecosystem.

Examples

Example of incorrect code:

// Using namespace keyword
namespace Validation {
  export interface StringValidator {
    isValid(s: string): boolean;
  }
  
  export class RegexValidator implements StringValidator {
    isValid(s: string): boolean {
      return true;
    }
  }
}

// Using module keyword (older syntax)
module Shipping {
  export interface Address {
    street: string;
    city: string;
  }
  
  export function validate(address: Address) {
    // ...
  }
}

// Nested namespaces
namespace App {
  namespace Models {
    export interface User {
      id: number;
      name: string;
    }
  }
}

Example of correct code:

// Using ES modules
export interface StringValidator {
  isValid(s: string): boolean;
}

export class RegexValidator implements StringValidator {
  isValid(s: string): boolean {
    return true;
  }
}

// Multiple exports
export interface Address {
  street: string;
  city: string;
}

export function validate(address: Address) {
  // ...
}

// Declare modules for external libraries
declare module 'external-lib' {
  export function helper(): void;
}

// Global augmentation in .d.ts files
declare global {
  interface Window {
    customProperty: string;
  }
}