2013-05-31 87 views
0

当尝试使用window.FormData我得到以下错误:打字稿不知道FORMDATA是什么

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

同样发生的FileReader

+0

它应该,因为有一个[问题](http://typescript.codeplex.com/workitem/720)关于它,已经修复。什么版本的TypeScript被使用? – raina77ow

+0

版本0.8.3.1 – localhost

+1

奇怪。好的,如果你使用没有'window'前缀的'FormData'? – raina77ow

回答

1

可以检查功能存在使用:

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

这依赖于错误的检查 - 如果你想明确,使用。

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

因为它引发异常,Falsey检查对于未定义不好。试试看:http://jsfiddle.net/basarat/2MJ8j/明确的一个是好的:) – basarat

+0

非答复的例子已经显示在答案中。 – Fenton

+0

我看到这个队友,但这个虚构的例子是错误的*“未定义的变量,比如'foo'。如果你在typeof以外的任何上下文中访问一个未定义的变量,你将会得到一个错误。 http://stackoverflow.com/a/2559513/390330 – basarat