sanfe.dev

TypeScript 베스트 프랙티스

실무에서 바로 적용할 수 있는 TypeScript 사용 패턴과 팁을 소개합니다.

TypeScript 베스트 프랙티스

TypeScript를 효과적으로 사용하기 위한 몇 가지 패턴을 소개합니다.

1. Strict Mode 활성화

{
  "compilerOptions": {
    "strict": true
  }
}

strict 모드는 다음을 포함합니다:

  • noImplicitAny
  • strictNullChecks
  • strictFunctionTypes

2. Type Guards 활용

function isString(value: unknown): value is string {
  return typeof value === 'string';
}

3. const assertions

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

4. Template Literal Types

type EventName = `on${Capitalize<string>}`;

TypeScript는 계속 발전하고 있으니 최신 기능을 주시하세요.