类型“Window & typeof globalThis”上不存在属性“showSaveFilePicker”

mas*_*son 18 window typescript reactjs eslint typescript-typings

const opts = {
      suggestedName: 'data',
      types: [
        {
          description: 'NewCSV',
          accept: { 'text/csv': ['.csv'] },
        },
      ],
    };
    const handle = await (window).showSaveFilePicker(opts);
Run Code Online (Sandbox Code Playgroud)

现在我有类型错误Property 'showSaveFilePicker' does not exit on type 'Window & typeof globalThis',提供 type as any解决了类型错误的问题。

const handle = await (<any>window).showSaveFilePicker(opts);
Run Code Online (Sandbox Code Playgroud)

但它仍然会显示 eslint 错误Unsafe call of an anytyped value。所以我知道的唯一解决方案是禁用该文件的 eslint。有没有其他方法可以避免类型错误和 eslint 错误?

Mer*_*n04 36

文件系统访问 API 的 TypeScript 类型定义目前似乎已损坏: https: //github.com/microsoft/vscode/issues/141908

现在,尝试安装包@types/wicg-file-system-access来修复类型:

# Yarn
yarn add --dev @types/wicg-file-system-access

# NPM
npm install --save-dev @types/wicg-file-system-access
Run Code Online (Sandbox Code Playgroud)

您还需要使用以下内容更新 tsconfig.json 的 types 字段:

  "compilerOptions": {
    "types": [ "@types/wicg-file-system-access"]
   }
Run Code Online (Sandbox Code Playgroud)

  • @Alexander刚刚意识到,它在 Angular 中对我有用,但是通过将类型添加到`tsconfig.app.json`中的compilerOptions,而不是`tsconfig.json` (2认同)