4

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

MiMo
  • 11,793
  • 1
  • 33
  • 48
localhost
  • 845
  • 3
  • 14
  • 32

2 Answers2

14

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

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

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');
}
basarat
  • 261,912
  • 58
  • 460
  • 511
Fenton
  • 241,084
  • 71
  • 387
  • 401
  • Falsey checks are bad for undefined since it throws an exception. Try it: http://jsfiddle.net/basarat/2MJ8j/ The explicit one is fine :) – basarat Jun 01 '13 at 23:11
  • I see that mate, but the falsey example is *wrong* "Undefined variables , like 'foo'. You'll get an error if you access an undefined variable in any context other than typeof." http://stackoverflow.com/a/2559513/390330 – basarat Jun 02 '13 at 23:26
  • 1
    you can use `if (window.FormData)` if you want the falsey check. `if (FormData)` is a straight up exception and not a functioning piece of code. Made the edit if thats okay :) – basarat Jun 02 '13 at 23:27