错误 TS2339:“FormData”类型上不存在属性“entries”

web*_*ial 10 typescript tslint angular

我在 Stackoverflow 和网络上搜索,我发现这个线程https://github.com/Microsoft/TypeScript/issues/14813但它没有解决我的问题。我在编译我的代码时遇到了这个错误ng serve --watch

错误 TS2339:“FormData”类型上不存在属性“entries”。

这部分fd.entries()触发了错误......知道这里发生了什么吗?

onFileSelected(event) {
    const fd = new FormData;

    for (const file of event.target.files) {
        fd.append('thumbnail', file, file.name);

        const entries = fd.entries();
        // some other methods are called here e. g. upload the images

        for (const pair of entries) {
            fd.delete(pair[0]);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我看到那里(https://github.com/Microsoft/TypeScript/blob/master/src/lib/dom.iterable.d.ts)是一些接口,entries但不知何故这对我不起作用。

编辑

我的tsconfig.json看起来像这样

{
    "compileOnSave": false,
    "compilerOptions": {
        "outDir": "./dist/out-tsc",
        "sourceMap": true,
        "declaration": false,
        "moduleResolution": "node",
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "target": "es5",
        "typeRoots": [
            "node_modules/@types"
        ],
        "lib": [
            "es2017",
            "dom"
        ]
    }
}
Run Code Online (Sandbox Code Playgroud)

gkr*_*kri 33

您需要将“dom.iterable”添加到您的lib列表中tsconfig.json,例如:

{
  "compilerOptions": {
    "baseUrl": ".",
    "outDir": "build/dist",
    "module": "esnext",
    "target": "es6",
    "lib": [
      "es2019",
      "dom",
      "dom.iterable" // <- there you are
    ],
    // ...
}
Run Code Online (Sandbox Code Playgroud)

  • 并再次“ng服务”。 (4认同)

Pad*_*han 9

除非您编译为 ES6,否则 TypeScript 不支持它。

"target": "es5"
Run Code Online (Sandbox Code Playgroud)

如果是es6的话就可以了。


Pie*_*Duc 3

entries并非所有浏览器都支持该方法。如果您无论如何都想使用该方法,并且仍然将目标设置为 es5,则可以扩展当前接口

declare global {
  interface FormData {
    entries(): Iterator<[USVString, USVString | Blob]>;
  }
}
Run Code Online (Sandbox Code Playgroud)