Skip to content
← Back to rules

typescript/no-unnecessary-boolean-literal-compare 의심스러운

💭 This rule requires type information.
An auto-fix is available for this rule.

작동 방식

이 규칙은 불필요한 부울 리터럴과의 동등 비교를 금지합니다.

왜 문제가 되는가?

비교 대상이 부울 값일 때, 부울 리터럴과의 비교는 제거할 수 있으므로 불필요합니다. 이러한 비교는 코드를 더 길게 만들지만, 추가적인 가치를 제공하지 않습니다.

예시

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

ts
declare const someCondition: boolean;

if (someCondition === true) {
  // ...
}

if (someCondition === false) {
  // ...
}

if (someCondition !== true) {
  // ...
}

if (someCondition !== false) {
  // ...
}

const result = someCondition == true;

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

ts
declare const someCondition: boolean;

if (someCondition) {
  // ...
}

if (!someCondition) {
  // ...
}

// 부울이 아닌 타입과의 비교는 허용됩니다
declare const someValue: unknown;
if (someValue === true) {
  // ...
}

구성

이 규칙은 다음 속성을 가진 구성 객체를 수용합니다.

allowComparingNullableBooleansToFalse

type: boolean

기본값: true

null을 포함할 수 있는 부울 표현식을 false와 비교하는 것을 허용할지 여부. false로 설정 시, xboolean | null인 경우 x === false는 경고 대상이 됩니다.

allowComparingNullableBooleansToTrue

type: boolean

기본값: true

null을 포함할 수 있는 부울 표현식을 true와 비교하는 것을 허용할지 여부. false로 설정 시, xboolean | null인 경우 x === true는 경고 대상이 됩니다.

사용 방법

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

json
{
  "rules": {
    "typescript/no-unnecessary-boolean-literal-compare": "error"
  }
}
bash
oxlint --type-aware --deny typescript/no-unnecessary-boolean-literal-compare

참고 자료