在Angular/TypeScript项目中,可以使用ESLint规则来确保代码中的函数不会产生副作用,即只返回新数据而不修改现有数据。这样可以提高代码的可测试性和可维护性。
具体实现的做法是在ESLint配置文件中添加如下规则:
{
"rules": {
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/ban-types": [
"error",
{
"types": {
"{}": false
}
}
],
"import/no-extraneous-dependencies": [
"error",
{
"devDependencies": ["**/*.test.ts", "**/*.spec.ts"]
}
],
"complexity": ["warn", 7],
"max-lines-per-function": ["warn", 25],
"max-nested-callbacks": ["warn", 3],
"max-params": ["warn", 4],
"max-depth": ["warn", 3],
"max-statements": ["warn", 15],
"max-lines": ["warn", 120],
"no-console": ["error"],
"no-param-reassign": [
"error",
{ "props": true, "ignorePropertyModificationsFor": ["memo"] }
],
"no-plusplus": ["error", { "allowForLoopAfterthoughts": true }],
"no-underscore-dangle": [
"error",
{
"allow": [
"__",
"_id"
]
}
],
"id-length": [
"error",
{
"min": 2,
"max": 40,
"properties": "never",
"exceptions": ["i", "j", "x", "y", "z"]
}
],
"prefer-const": ["warn"],
"camelcase": [
"error",
{
"properties": "always",
"ignoreDestructuring": false,
"ignoreImports": false,
"allow": []
}
],
"valid-jsdoc