ESLint:打字稿中未定义“部分”

Fai*_*zer 5 pycharm typescript reactjs eslint typescript-eslint

ESLint 无法识别Partial打字稿,但编译后的模块没有给出任何错误。

const initialState: IAuthState = {
  authenticated: false,
  processing: false,
};

const authReducer = (state: IAuthState = initialState, action: any): IAuthState => {
  const State = (newState: Partial<IAuthState>): IAuthState => ({...state, ...newState});

  switch (action.type) {
    case Actions.SIGN_IN_PROCESS_INITIATED:
      return State({processing: true});

    case Actions.SIGN_IN_PROCESS_FAILED:
      return State({processing: false});

    default:
      return state;
  }
};
Run Code Online (Sandbox Code Playgroud)

我知道这可以被抑制,// eslint-disable-next-line no-undef但我仍然想要一个解释和一个永久的解决方案,这样我就不会得到这个错误Error

小智 8

我前段时间遇到过类似的情况,并通过安装@typescript-eslint/parser为 devDependency 并将其包含在 eslint 配置中来修复它:

  "extends": [..., "@typescript-eslint/parser"],
Run Code Online (Sandbox Code Playgroud)

  • 文档说它需要在 eslint 配置中用作 `parser` 而不是 `extends`,如下所示: "parser": "@typescript-eslint/parser" (8认同)