即使输入了函数,我也会在导入的函数上出现 no-unsafe-call 和 no-unsafe-assignment eslint 错误

Mik*_*ike 7 typescript eslint

导入类型化函数时,我收到 no-unsafe-call 和 no-unsafe-assignment eslint 错误。如果函数在同一个文件中声明,错误就会消失。eslint 似乎无法获取导入函数的返回类型。

在使用WorkspaceMessages.ts

export interface WorkspaceMessage {
  message: string;
  created: string;
}

export default function useWorkspaceMessages(): WorkspaceMessage[] {
  return [];
}
Run Code Online (Sandbox Code Playgroud)

在 app.tsx

import useWorkspaceMessages, {
  WorkspaceMessage,
} from 'useWorkspaceMessages';

const workspaceMessages: WorkspaceMessage[] = useWorkspaceMessages();
     ^^^^^^^^^^^^^^^^^^^                      ^^^^^^^^^^^^^^^^^^^^
error  Unsafe assignment of an any value  @typescript-eslint/no-unsafe-assignment
error  Unsafe call of an any typed value  @typescript-eslint/no-unsafe-call
Run Code Online (Sandbox Code Playgroud)

如果我在 app.tsx 中声明useWorkspaceMessagesWorkspaceMessage接口,错误就会消失。

Mik*_*ike 6

弄清楚出了什么问题。我犯了一个错误.eslint.js

parserOptions.project指向了错误的目录tsconfig.json。遗憾的是,eslint 并没有抱怨这一点。

  • 我遇到了同样的错误,但据我所知,我的“parserOptions.project”指向正确的“tsconfig.json”。您能用 ESLint 和 Typescript 配置来扩展您的答案吗? (3认同)