Javascript 打字稿错误 TS2339:“窗口”类型上不存在属性“webkitURL”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38802171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Typescript error TS2339: Property 'webkitURL' does not exist on type 'Window'
提问by Dan Reil
Using Angular 2 on a project which is compiled with typescript.
在用打字稿编译的项目上使用 Angular 2。
Getting this error when trying to create a blob image:
尝试创建 blob 图像时出现此错误:
error TS2339: Property 'webkitURL' does not exist on type 'Window'
error TS2339: Property 'webkitURL' does not exist on type 'Window'
ts code is:
ts代码是:
public url = window.URL || window.webkitURL;
this.photo = this.url.createObjectURL( res );
public url = window.URL || window.webkitURL;
this.photo = this.url.createObjectURL( res );
回答by basarat
error TS2339: Property 'webkitURL' does not exist on type 'Window'
错误 TS2339:“窗口”类型上不存在属性“webkitURL”
The lib.d.ts does not ship with stuff that is browser specific. However you can easily do (window as any).webkitURL
. This is called a type assertion.
lib.d.ts 不附带特定于浏览器的内容。但是,您可以轻松做到(window as any).webkitURL
。这称为类型断言。
More
更多的
The common (as any)
style type assertion is a quickfix provided by alm : https://basarat.gitbooks.io/alm/content/features/quickfix.html
通用(as any)
样式类型断言是 alm 提供的 quickfix:https://basarat.gitbooks.io/alm/content/features/quickfix.html
回答by Dan Reil
The solution that works as of TypeScript 2.1.5:
适用于 TypeScript 2.1.5 的解决方案:
interface Window {
webkitURL?: any;
}
declare var window: Window;
if (window.webkitURL !== undefined) {
console.log(window.webkitURL);
}
In the above code we declared an interface/shape for Window which will optionally have webkitURL defined and then we do a check to make sure it is defined.
在上面的代码中,我们为 Window 声明了一个接口/形状,它可以选择定义 webkitURL,然后我们进行检查以确保它已定义。
回答by Artem Bogdan
This approach worked for me. My curren version of typescript is 2.0.3
这种方法对我有用。我当前的打字稿版本是 2.0.3
add this out of the class
将这个添加到课堂之外
interface Window { logged_user: Object }
and when you need use this property, just use it
当您需要使用此属性时,只需使用它
window.logged_user = {};//your data
回答by Imamudin Naseem
Solution 1:
解决方案1:
interface Window {
webkitURL: any;
}
declare var window: Window;
//Now typescript will not throw any error on window.webkitURL
Solution 2:
(<any>window).webkitURL
does not throw ts error
解决方案2:
(<any>window).webkitURL
不抛出ts错误