typescript/no-unnecessary-condition Nursery
작동 방식
타입스크립트의 타입 정보를 기반으로 항상 참이거나 항상 거짓이거나 항상 비정의값인 조건을 금지합니다.
왜 좋지 않은가?
런타임 시 변화가 없이 고정된 조건은 코드를 더 어렵게 만들며, 로직 오류를 숨길 수 있습니다. 종종 비활성 브랜치를 남기고, 선언된 타입이 의도한 동작과 일치하지 않는다는 신호를 줍니다.
예시
이 규칙에 부적절한 코드 예시:
ts
declare const value: null;
if (value) {
doWork();
}
const items: string[] = [];
if (items) {
doWork();
}
declare const status: "ready";
if (!status) {
reportError();
}이 규칙에 적절한 코드 예시:
ts
declare const maybeUser: User | undefined;
if (maybeUser) {
doWork(maybeUser);
}
const items: string[] = [];
if (items.length > 0) {
doWork();
}
declare const status: "ready" | "";
if (!status) {
reportError();
}구성
이 규칙은 다음 속성을 가진 구성 객체를 수락합니다.
allowConstantLoopConditions
type: boolean | "never" | "always" | "only-allowed-literals"
allowConstantLoopConditions가 JSON에서 지정될 수 있는 다양한 방식을 나타냅니다. 다음 중 하나일 수 있습니다:
true또는false- 문자열 열거형 (
"never","always","only-allowed-literals")
checkTypePredicates
type: boolean
기본값: false
타입 예측 함수를 확인할지 여부.
사용 방법
이 규칙을 활성화하려면 설정 파일이나 명령줄 인터페이스를 통해 다음을 사용할 수 있습니다:
json
{
"rules": {
"typescript/no-unnecessary-condition": "error"
}
}bash
oxlint --type-aware --deny typescript/no-unnecessary-condition