no-unsafe-function-type
Disallow using the unsafe built-in Function type.
Extending "plugin:@typescript-eslint/recommended"
in an ESLint configuration enables this rule.
Some problems reported by this rule are automatically fixable by the --fix
ESLint command line option.
TypeScript's built-in Function
type allows being called with any number of arguments and returns type any
.
Function
also allows classes or plain objects that happen to possess all properties of the Function
class.
It's generally better to specify function parameters and return types with the function type syntax.
"Catch-all" function types include:
() => void
: a function that has no parameters and whose return is ignored(...args: never) => unknown
: a "top type" for functions that can be assigned any function type, but can't be called
Examples of code for this rule:
- ❌ Incorrect
- ✅ Correct
let noParametersOrReturn: Function;
noParametersOrReturn = () => {};
let stringToNumber: Function;
stringToNumber = (text: string) => text.length;
let identity: Function;
identity = value => value;
Open in Playgroundlet noParametersOrReturn: () => void;
noParametersOrReturn = () => {};
let stringToNumber: (text: string) => number;
stringToNumber = text => text.length;
let identity: <T>(value: T) => T;
identity = value => value;
Open in Playground- Flat Config
- Legacy Config
export default tseslint.config({
rules: {
"@typescript-eslint/no-unsafe-function-type": "error"
}
});
module.exports = {
"rules": {
"@typescript-eslint/no-unsafe-function-type": "error"
}
};
Try this rule in the playground ↗
Options
This rule is not configurable.
When Not To Use It
If your project is still onboarding to TypeScript, it might be difficult to fully replace all unsafe Function
types with more precise function types.
You might consider using ESLint disable comments for those specific situations instead of completely disabling this rule.