避免在React + Typescript中使用通用函数类型

问题描述

我正在寻找防止将“函数”用作类型的规则

myMethod: Function;

我什么都没找到,所以我愿意接受建议:)

解决方法

遵循规则Function类型,您可以使用eslint

https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/ban-types.md

,

您可以使用@typescript-eslint/ban-types规则Rule Link。但这也会禁止其他默认类型,例如StringBoolean等。Link of other default banned types

如果您只想禁止Function并禁用其他禁止,则将其添加到您的.eslintrc文件中。

"@typescript-eslint/ban-types": ["error",{
        "types": {
            "String": false,"Boolean": false,"Number": false,"Symbol": false,"{}": false,"Object": false,"object": false,"Function": true,},"extendDefaults": true
    }
]