3

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

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

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.
4

1 回答 1

5

行为上的差异是由于 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选项。

于 2020-06-22T02:58:11.617 回答