typescript/no-unsafe-function-type 严谨
它的作用
禁止使用不安全的内置 Function 类型。
为什么这是个问题?
TypeScript 的内置 Function 类型允许以任意数量的参数调用,并返回类型 any。Function 还允许类或普通对象,只要它们恰好拥有 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