Skip to content
← Back to rules

typescript/consistent-type-definitions 스타일

⚠️🛠️ A dangerous auto-fix is available for this rule for some violations.

작동 방식

interface 또는 type 중 하나를 일관되게 사용하여 타입 정의를 강제합니다.

왜 문제가 되는가?

TypeScript는 객체 타입을 정의하는 두 가지 일반적인 방법을 제공합니다: interfacetype.
이 둘은 일반적으로 매우 유사하며, 대부분 상호 교환 가능합니다.
같은 타입 선언 스타일을 일관되게 사용하면 코드 가독성이 향상됩니다.

예시

기본적으로 이 규칙은 객체 타입을 정의할 때 interface 사용을 강제합니다.

이 규칙에 부적절한 코드 예시:

typescript
type T = { x: number };

이 규칙에 적절한 코드 예시:

typescript
type T = string;
type Foo = string | {};

interface T {
  x: number;
}

구성

이 규칙은 다음 중 하나의 문자열 값을 수용합니다.

"interface"

객체 타입 정의 시 interfacetype보다 우선합니다:

typescript
interface T {
  x: number;
}

"type"

객체 타입 정의 시 typeinterface보다 우선합니다:

typescript
type T = { x: number };

사용 방법

구성 파일이나 명령줄 인터페이스에서 이 규칙을 활성화하려면 다음을 사용할 수 있습니다:

json
{
  "rules": {
    "typescript/consistent-type-definitions": "error"
  }
}
bash
oxlint --deny typescript/consistent-type-definitions

참고 자료