【问题标题】:Property 'text' does not exist on type 'File'“文件”类型上不存在属性“文本”
【发布时间】:2020-10-07 22:30:00
【问题描述】:

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

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.

【问题讨论】:

    标签: typescript visual-studio-code eslint webpack-encore typescript-eslint


    【解决方案1】:

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

    【讨论】:

    • 太棒了!我在一个项目中遇到了同样的问题,该项目使用的 TS 版本与 VS Code 使用的版本不同,并且我在使用 npm start 时遇到编译错误,但 VS Code 很高兴哈哈! (而且该项目似乎运行良好)
    猜你喜欢
    • 2021-06-18
    • 1970-01-01
    • 2019-11-24
    • 2019-12-24
    • 1970-01-01
    • 2021-05-29
    • 2018-08-02
    • 1970-01-01
    • 2017-11-03
    相关资源
    最近更新 更多