我在 React 项目中使用 eslint、prettier、vite。
我已将 VSCode 工作区设置设置为"source.fixAll.eslint"
和。但它不断更改为自动,例如当我打开 VSCode 或当我编码时。"source.organizeImports"
true
"explicit"
可能是什么原因?
// .vscode/settings.json (workspace settings)
{
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true, // keeps automatically changing to "explicit"
"source.organizeImports": true // keeps automatically changing to "explicit"
},
"editor.formatOnSave": true,
"editor.wordWrap": "on",
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
Run Code Online (Sandbox Code Playgroud)
// package.json devDependencies
"devDependencies": {
"@testing-library/react": "^14.0.0",
"@types/jest": "^29.5.6",
"@types/react": "^18.2.15",
"@types/react-dom": "^18.2.7",
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
"@vitejs/plugin-react": "^4.0.3",
"eslint": "^8.45.0",
"eslint-plugin-prettier": "^5.0.1",
"eslint-plugin-react": "^7.33.2",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.3",
"husky": "^8.0.3",
"jest": "^29.7.0",
"jest-environment-jsdom": …
Run Code Online (Sandbox Code Playgroud) 我打算changeSelectedItem
成为其参数具有不同类型的两个函数之一。
type Props = {
changeSelectedItem:
| ((value: string) => void)
| ((item: { displayValue: ReactNode; value: string }) => void);
}
function Component({ changeSelectedItem }: Props) {
const onChange = (
event: SelectChangeEvent<
string | { displayValue: ReactNode; value: string }
>
) => {
const selectedValue = event.target.value;
changeSelectedItem(selectedValue);
};
// ...
}
Run Code Online (Sandbox Code Playgroud)
但当使用它时,它的类型就变成了交集。
// TS inferred type of `changeSelectedItem`.
(parameter) changeSelectedItem: (arg0: string & {
displayValue: ReactNode;
value: string;
}) => void
Run Code Online (Sandbox Code Playgroud)
这导致了这个错误。
// Error …
Run Code Online (Sandbox Code Playgroud)