ESLint: 在 TypeScript 中未定义 'Partial'。

6

ESLint无法识别typescript中的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;
  }
};

我知道可以通过// eslint-disable-next-line no-undef来禁止该错误,但我仍然想要一个解释和一个永久的解决方案,以便我不会再遇到这个不太好的错误


这感觉就像是 eslint 配置中 Typescript 解析器没有正确设置。你有检查过吗? - Christian Ivicevic
2
你能建议需要哪些配置吗? - Faisal Manzer
2个回答

8

我之前遇到过类似的情况,并通过将@typescript-eslint/parser作为devDependency安装并在eslint配置中包含它来解决:

  "extends": [..., "@typescript-eslint/parser"],

8
文档指出在 eslint 配置中需使用 parser 而非 extends,示例如下: "parser": "@typescript-eslint/parser"。 - jamland

0

在逐一尝试其他compilerOptions配置参数后,我发现对我来说问题出在我没有在compilerOptions.lib下列出esnext

{
  "compilerOptions": {
    // other config
    "lib": [
      "dom",
      "dom.iterable",
      "esnext" // I added this to ensure `Partial`, `Record`, and others were recognized as defined by ESLint
    ]
    // other config
  }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接