j4flmao/agent_skills_nodejs_nestjs

designing-oop-typescript

Designs classes and class hierarchies in TypeScript using SOLID, encapsulation, and immutability.

소스 보기

사용 방법

  1. 설치 명령을 복사하고 자신의 에이전트 환경에 설치하기 전에 소스 패키지를 확인하세요.
  2. 필요한 도구, 인증 정보, 지원 환경은 원본 문서에서 확인하세요.
  3. 입력과 원하는 산출물을 설명하고 출처와 결과를 대조하세요.
npx skills add https://github.com/j4flmao/agent_skills_nodejs_nestjs --skill designing-oop-typescript

시작 전 확인

  • 설치에는 모델 구독, API 한도, 파일 접근 권한이 포함되지 않습니다.
  • 별점 수와 설치 수는 활동 지표이며 독립적인 품질·보안 검증이 아닙니다.
원본 Skill 문서

원본 저장소의 제목, 예시, 코드, 표, 링크, 이미지를 유지해 표시합니다.

Designing OOP TypeScript

When to use

  • Creating new domain entities or value objects
  • Refactoring procedural code into OOP
  • Designing class hierarchies with SOLID principles

Core rules

  1. Classes over functions: all behavior encapsulated in classes
  2. Explicit access modifiers: public, protected, private always written
  3. SRP: one responsibility per class, split when growing
  4. Encapsulation: no public mutable fields, expose intent via methods
  5. Immutability: value objects are readonly, entities use private state
  6. SOLID: Single responsibility, Open/closed, Liskov, Interface segregation, Dependency inversion

Reference shape (TypeScript)

Domain Entity

typescript
export class User {
  private constructor(
    private readonly _id: string,
    private _email: string,
    private _isActive: boolean
  ) {}

  static create(id: string, email: string): User {
    return new User(id, email, true);
  }

  get id(): string { return this._id; }
  get email(): string { return this._email; }

  deactivate(): void {
    if (!this._isActive) throw new BusinessError('ALREADY_INACTIVE', 'User already inactive');
    this._isActive = false;
  }
}

Value Object

typescript
export class Email {
  private constructor(private readonly value: string) {}

  static create(email: string): Email {
    if (!PATTERNS.EMAIL.test(email)) throw new ValidationError([{ field: 'email', message: 'Invalid' }]);
    return new Email(email.toLowerCase());
  }

  equals(other: Email): boolean {
    return this.value === other.value;
  }
}

Examples — Do

typescript
class Order {
  private items: OrderItem[] = [];
  addItem(item: OrderItem): void { this.items.push(item); }
  get total(): number { return this.items.reduce((sum, i) => sum + i.price, 0); }
}

Examples — Don't

typescript
// ❌ Function module, no encapsulation
export function createUser(id: string, email: string) { return { id, email }; }
// ❌ Public mutable fields
class User { id: string; email: string; }

Checklist

  • [ ] Classes with explicit access modifiers
  • [ ] No public mutable fields
  • [ ] Value objects are immutable
  • [ ] Entities encapsulate behavior
  • [ ] SOLID principles applied

See reference/oop-patterns.md for full patterns.

관련 작업

같은 저장소의 Skills

더 많은 Skills

모든 Skills