typescript/no-unsafe-function-type 철두철미
작동 방식
안전하지 않은 내장된 Function 타입 사용을 금지합니다.
왜 문제가 되는가?
TypeScript의 내장 Function 타입은 임의의 개수의 인자를 가질 수 있고, 반환 타입이 any입니다. 또한 Function 클래스의 모든 속성을 가진 클래스 또는 일반 객체도 허용됩니다. 일반적으로 함수 매개변수와 반환 타입을 명시하기 위해 함수 타입 구문을 사용하는 것이 더 좋습니다.
예시
이 규칙에 위반되는 잘못된 코드 예시:
ts
let noParametersOrReturn: Function;
noParametersOrReturn = () => {};
let stringToNumber: Function;
stringToNumber = (text: string) => text.length;
let identity: Function;
identity = (value) => value;이 규칙에 맞는 올바른 코드 예시:
ts
let noParametersOrReturn: () => void;
noParametersOrReturn = () => {};
let stringToNumber: (text: string) => number;
stringToNumber = (text) => text.length;
let identity: <T>(value: T) => T;
identity = (value) => value;사용 방법
이 규칙을 구성 파일이나 CLI에서 활성화하려면 다음을 사용할 수 있습니다:
json
{
"rules": {
"typescript/no-unsafe-function-type": "error"
}
}bash
oxlint --deny typescript/no-unsafe-function-type