localhost
localhost

Reputation: 921

Typescript doesn't know what FormData is

When trying to use window.FormData I get the following error:

The name 'FormData' does not exist in the current scope

The same happens to FileReader

Upvotes: 4

Views: 8354

Answers (2)

Behzad B. Mokhtari
Behzad B. Mokhtari

Reputation: 251

add dom to the lib array in the tsconfig.json of your project.

{
  "compilerOptions": {
    ...
    "lib": ["es2018", "dom"], // add `dom` to the array
    ...
  }
}

Upvotes: 17

Fenton
Fenton

Reputation: 250942

You can check a feature exists using:

if (window.FormData) {
    alert('Yes');
}

This relies on falsey checks - if you want to be explicit, use.

if (typeof FormData !== 'undefined') {
    alert('Yes');
}

Upvotes: 1

Related Questions