Disallow using the unsafe built-in Function type.
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 calledExamples of code for this rule:
let noParametersOrReturn: Function;
noParametersOrReturn = () => {};
let stringToNumber: Function;
stringToNumber = (text: string) => text.length;
let identity: Function;
identity = value => value;
Open in Playground
let noParametersOrReturn: () => void;
noParametersOrReturn = () => {};
let stringToNumber: (text: string) => number;
stringToNumber = text => text.length;
let identity: <T>(value: T) => T;
identity = value => value;
Open in Playground
eslint.config.mjs
export default tseslint.config({
rules: {
"@typescript-eslint/no-unsafe-function-type": "error"
}
});
.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/no-unsafe-function-type": "error"
}
};
Try this rule in the playground ↗
OptionsThis rule is not configurable.
When Not To Use ItIf 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.
any
s with Linting and TypeScriptno-empty-object-type
no-restricted-types
no-unsafe-call
no-wrapper-object-types
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4