简体   繁体   English

打字稿错误TS2339:'Window'类型中不存在属性'webkitURL'

[英]Typescript error TS2339: Property 'webkitURL' does not exist on type 'Window'

Using Angular 2 on a project which is compiled with typescript. 在使用typescript编译的项目上使用Angular 2。

Getting this error when trying to create a blob image: 尝试创建blob图像时出现此错误:

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 );

error TS2339: Property 'webkitURL' does not exist on type 'Window' 错误TS2339:类型'Window'上不存在属性'webkitURL'

The lib.d.ts does not ship with stuff that is browser specific. lib.d.ts不附带浏览器特定的内容。 However you can easily do (window as any).webkitURL . 但是您可以轻松地执行(window as any).webkitURL This is called a type assertion . 这称为类型断言

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

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,然后我们进行检查以确保定义它。

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

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错误

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeScript:错误TS2339:类型“ Window”上不存在属性“ CustomEvent” - TypeScript: error TS2339: Property 'CustomEvent' does not exist on type 'Window' 错误TS2339:类型“ {}”上不存在属性“ forEach” - error TS2339: Property 'forEach' does not exist on type '{}' 错误TS2339:类型“ HTMLElement”上不存在属性“名称” - error TS2339: Property 'name' does not exist on type 'HTMLElement' 错误 TS2339:“温度”类型上不存在属性“摄氏度” - error TS2339: Property 'celsius' does not exist on type 'Temperature' 错误 TS2339:“特殊 []”类型上不存在属性“默认” - Error TS2339: Property 'default' does not exist on type 'Special[]' 错误TS2339:类型“ HTMLElement”上不存在属性“ imageID” - error TS2339: Property 'imageID' does not exist on type 'HTMLElement' 错误TS2339:类型“字符串”上不存在属性“默认”。** - Error TS2339: Property 'Default' does not exist on type 'string'.** Angular2打字稿错误TS2339:类型“ Y”上不存在属性“ x” - Angular2 typescript error TS2339: Property 'x' does not exist on type 'Y' 如何解决打字稿错误 TS2339:“IntrinsicAttributes &amp; …”类型上不存在“XXX”属性? - How to resolve typescript error TS2339: Property 'XXX' does not exist on type 'IntrinsicAttributes & …? TypeScript 错误:属性“scrollIntoView”在类型“never”上不存在。 TS2339 - TypeScript error: Property 'scrollIntoView' does not exist on type 'never'. TS2339
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM