“文件”类型上不存在属性“文本”

Are*_*res 4 typescript eslint visual-studio-code webpack-encore typescript-eslint

我有以下代码来处理用户的文件上传:

17  const onFileDrop =
18    (files: File[]) => {
19      files.forEach(file => file.text().then(content => console.log(content)));
20    },
21  );
Run Code Online (Sandbox Code Playgroud)

VS Code 没有显示任何错误,我可以正确访问File包含text()函数的界面。

在此输入图像描述 在此输入图像描述

但是,运行时npm start我看到以下错误:

[tsl] ERROR in {omitted}/RawCommandOutputs.tsx(19,34)
      TS2339: Property 'text' does not exist on type 'File'.


[tsl] ERROR in {omitted}/RawCommandOutputs.tsx(19,46)
      TS7006: Parameter 'content' implicitly has an 'any' type.
Run Code Online (Sandbox Code Playgroud)

Are*_*res 6

行为差异是由于 webpack 和 VS Code 使用不同版本的 Typescript 造成的。

该项目依赖于 Typescript 版本 3.5.2。在此版本中,该File界面不包含任何text()功能。因此,运行时npm start显示上述错误。

VS Code 似乎默认使用最新版本的 Typescript(在我的例子中为 3.9.5),其中包含更新的File界面。

为了使 VS Code 使用与我的项目相同版本的 Typescript,我将其添加
"typescript.tsdk": "./node_modules/typescript/lib"到我的settings.json文件中。然后,我运行Select Typescript Version命令并选择选项Use Workspace Version