eslint/valid-typeof 정확성
동작 방식
typeof 표현식을 유효한 문자열과 비교하도록 강제합니다.
왜 나쁜가요?
대부분의 사용 사례에서 typeof 연산자의 결과는 다음 문자열 리터럴 중 하나입니다: "undefined", "object", "boolean", "number", "string", "function", "symbol", "bigint"입니다. typeof 연산자의 결과를 다른 문자열 리터럴과 비교하는 것은 보통 타입 오류입니다.
예시
이 규칙에 잘못된 코드 예시:
js
typeof foo === "strnig";
typeof foo == "undefimed";
typeof bar != "nunber"; // spellchecker:disable-line
typeof bar !== "fucntion"; // spellchecker:disable-line이 규칙에 올바른 코드 예시:
js
typeof foo === "string";
typeof bar == "undefined";
typeof foo === baz;
typeof bar === typeof qux;구성
이 규칙은 다음 속성을 가진 구성 객체를 받습니다.
requireStringLiterals
type: boolean
기본값: false
requireStringLiterals 옵션이 true로 설정되면, typeof 표현식을 문자열 리터럴 또는 다른 typeof 표현식만과 비교할 수 있으며, 다른 어떤 값과도 비교할 수 없습니다. 기본값은 false입니다.
requireStringLiterals가 true로 설정된 경우, 다음은 잘못된 코드 예시입니다:
js
typeof foo === undefined;
typeof bar == Object;
typeof baz === "strnig";
typeof qux === "some invalid type";
typeof baz === anotherVariable;
typeof foo == 5;requireStringLiterals가 true로 설정된 경우, 다음은 올바른 코드 예시입니다:
js
typeof foo === "undefined";
typeof bar == "object";
typeof baz === "string";
typeof bar === typeof qux;사용 방법
이 규칙을 구성 파일 또는 명령줄 인터페이스에서 활성화하려면 다음을 사용할 수 있습니다:
json
{
"rules": {
"valid-typeof": "error"
}
}bash
oxlint --deny valid-typeof