我正在使用 eslint 设置一个新的打字稿项目。我正在尝试正确设置 eslint 规则,以便tsc命令运行时不会出现错误。我在 中使用的“noImplicitAny”规则有问题tsconfig.json,但我无法在 eslint 中检查这一点。
.eslintrc.js:
module.exports = {
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
],
parser: "@typescript-eslint/parser",
parserOptions: {
project: ["tsconfig.json"],
sourceType: "module",
},
rules: {
"no-undef": "warn",
},
plugins: ["@typescript-eslint"],
settings: {
"import/resolver": {
node: {
extensions: [".js", ".ts"],
},
},
},
};
Run Code Online (Sandbox Code Playgroud)
tsconfig.json:
{
"compilerOptions": {
"target": "es5",
"module": "ES6",
"declaration": true,
"outDir": "./lib",
"strict": true,
"noImplicitAny": true
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules", "**/__tests__/*"]
}
Run Code Online (Sandbox Code Playgroud)
简而言之,我希望 eslint 检查隐式的 any 并警告其使用情况。我如何配置规则来.eslintrc.js …
有没有办法只解构函数参数中的某些对象属性,而其余的属性仍可在对象中访问?
考虑以下反应示例。(我以React为例,但这个问题一般适用于JS)
const Form = (formProps) => {
return (
...
<FormSectionComponent
initialValues={...}
values={...}
{...formProps}
/>
...
)
}
const FormSectionComponent = ({ initialValues, values}) => {
...
}
Run Code Online (Sandbox Code Playgroud)
传入的内容props在函数参数中被解构,但是,还有其他道具进来,我可能想在某些条件下访问这些道具,但我不想或无法解构 - 例如,我不知道它们是什么并且想记录他们。
有没有办法不解构参数部分中的其他道具并将它们作为props对象访问?
我能想到的唯一解决方法是:
const FormSectionComponent = (props) => {
const { initialValues, values} = props;
}
Run Code Online (Sandbox Code Playgroud)
但我想知道是否还有其他解决方案。