最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Typescript doesn't know what FormData is - Stack Overflow

programmeradmin0浏览0评论

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

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

Share Improve this question edited Jun 1, 2013 at 21:21 MiMo 12k1 gold badge34 silver badges48 bronze badges asked May 31, 2013 at 22:51 localhostlocalhost 9213 gold badges15 silver badges32 bronze badges 4
  • It should, as there's an issue about it, fixed already. What version of TypeScript is used? – raina77ow Commented May 31, 2013 at 22:57
  • 1 Weird. Ok, what if you use FormData without window prefix? – raina77ow Commented May 31, 2013 at 23:06
  • What browser are you using ? – Russ Clarke Commented May 31, 2013 at 23:07
  • Ah, so it does recognize FormData, but now how can I check if the browser supports it? Im using code that checks for window.FormData to search for browser support. – localhost Commented May 31, 2013 at 23:10
Add a comment  | 

2 Answers 2

Reset to default 17

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

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

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');
}
发布评论

评论列表(0)

  1. 暂无评论