【发布时间】:2022-01-14 14:04:17
【问题描述】:
.eslintrc.js
module.exports = {
root: true,
extends: [
'@react-native-community',
'standard-with-typescript',
'plugin:@typescript-eslint/recommended',
'plugin:jest/recommended',
'plugin:prettier/recommended',
'plugin:import/recommended',
'plugin:import/typescript',
],
plugins: ['@typescript-eslint', 'prettier'],
parser: '@typescript-eslint/parser',
ignorePatterns: [
'index.js',
'metro.config.js',
'react-native.config.js',
'reactotron.config.js',
'babel.config.js',
],
parserOptions: {
ecmaFeatures: {
jsx: true,
},
project: './tsconfig.json',
},
rules: {
'prefer-const': 2,
'no-var': 2,
'no-new-object': 2,
'object-shorthand': 2,
'no-useless-rename': 2,
'no-prototype-builtins': 2,
'no-array-constructor': 2,
'dot-notation': 0,
semi: 0,
'padding-line-between-statements': [
'error',
{ blankLine: 'always', prev: '*', next: 'return' },
{ blankLine: 'always', prev: '*', next: 'break' },
{ blankLine: 'always', prev: '*', next: 'continue' },
{ blankLine: 'always', prev: '*', next: 'function' },
{ blankLine: 'always', prev: '*', next: 'block' },
],
'lines-around-comment': [
2,
{
beforeLineComment: true,
allowBlockStart: true,
allowObjectStart: true,
allowObjectEnd: true,
},
],
'no-console': 1,
'no-param-reassign': [
'error',
{ props: true, ignorePropertyModificationsFor: ['state'] },
],
// @react-native-community config is outdated as we have upgraded typescript
// ^^ due to the above, all enums started throwing warning
'no-shadow': 'off',
// Import rules
'import/order': [
'error',
{
groups: [
'builtin',
'external',
'internal',
'parent',
'sibling',
'index',
'object',
'type',
],
alphabetize: {
order: 'asc',
},
'newlines-between': 'always',
},
],
'import/no-named-as-default-member': 'off',
// typescript rules
'@typescript-eslint/no-shadow': ['error'],
'@typescript-eslint/strict-boolean-expressions': 'off',
'@typescript-eslint/explicit-function-return-type': [
'error',
{
allowExpressions: true,
allowTypedFunctionExpressions: true,
},
],
'@typescript-eslint/naming-convention': [
'error',
{
selector: 'property',
format: ['snake_case', 'strictCamelCase', 'UPPER_CASE'],
},
],
},
settings: {
'import/parsers': {
'@typescript-eslint/parser': ['.ts', '.tsx'],
},
'import/resolver': {
typescript: {
alwaysTryTypes: true,
},
},
},
};
Store.ts
import { configureStore } from '@reduxjs/toolkit';
import usersReducer from '../features/users';
const store = configureStore({
reducer: {
users: usersReducer,
},
});
export type AppDispatch = typeof store.dispatch;
export type RootState = ReturnType<typeof store.getState>;
export default store;
useStore.ts
import { TypedUseSelectorHook, useDispatch, useSelector } from 'react-redux';
import { RootState, AppDispatch } from '../store/index';
export const useAppDispatch = () => useDispatch<AppDispatch>();
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;
() => useDispatch<AppDispatch>() 期望我在启用@typescript-eslint/explicit-function-return-type 时提供返回类型。将鼠标悬停在 VSCode 中的常量 useAppDispatch 上可以看到返回类型,但它需要经常更新,因为它会列出商店状态。
在不禁用规则的情况下,如何使函数避免要求返回类型,因为它已经推断出来了?谢谢。
包:
"react-redux": "^7.2.6",
"@types/react-redux": "^7.1.22",
"typescript": "^4.5.4",
【问题讨论】:
-
看看this是否有帮助。
-
@NalinRanjan 这行不通。我将分享我的 eslint 配置。
-
你可以只为那一行禁用 eslint。
// eslint-disable-next-line
标签: javascript reactjs typescript react-redux redux-toolkit