简体   繁体   English

function useAppDispatch 上缺少返回类型

[英]Missing return type on function useAppDispatch

.eslintrc.js .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 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 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>() expects me to provide a return type as @typescript-eslint/explicit-function-return-type is enabled. () => useDispatch<AppDispatch>()希望我提供返回类型,因为@typescript-eslint/explicit-function-return-type已启用。 The return type can be seen by hovering over the constant useAppDispatch in VSCode but it would require frequent updates as it's listing the store states.通过将鼠标悬停在 VSCode 中的常量useAppDispatch上可以看到返回类型,但它需要频繁更新,因为它列出了商店状态。

How can I make the function avoid asking for return type as it's already inferred without disabling the rule?如何使 function 避免要求返回类型,因为它已经推断出而不禁用规则? Thank you.谢谢你。

Packages:套餐:

"react-redux": "^7.2.6",
"@types/react-redux": "^7.1.22",
"typescript": "^4.5.4",

Disable the rule, at least in this line.至少在这一行禁用规则。 It is what it is.就是这样。

Seriously, this rule is harmful.说真的,这条规则是有害的。 You do not need to have each and every existing eslint rule active and rules that force annotation with certain types are especially harmful, causing you to remove available type information.您不需要激活每个现有的 eslint 规则,并且强制使用某些类型进行注释的规则尤其有害,导致您删除可用的类型信息。

If something is wrong, you will still get a type error, just at usage of the function, not at definition.如果出现问题,您仍然会收到类型错误,只是在使用 function 时,而不是在定义时。

There is one place where you need to annotate types and that is in function input positions - because there they cannot be inferred.有一个地方需要对类型进行注释,那就是在 function 输入位置 - 因为在那里无法推断它们。 Everything else is optional.其他一切都是可选的。 Sometimes it helps readability, sometimes it actively hinders it.有时它有助于可读性,有时它会积极阻碍它。 Use your mind at that point, don't stick to a fixed rule.在这一点上用你的头脑,不要坚持一个固定的规则。

Especially with Redux Toolkit, you get a lot of very specific types and manually annotating those will require you to either duplicate your code in types, or to annotate it in a way that you lose valuable information.尤其是使用 Redux 工具包,您会获得许多非常具体的类型,并且手动注释这些类型将需要您在类型中复制代码,或者以丢失有价值信息的方式对其进行注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM