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

Dan*_*eil 17 javascript webkit google-chrome typescript angular

在使用typescript编译的项目上使用Angular 2.

尝试创建blob图像时出现此错误:

error TS2339: Property 'webkitURL' does not exist on type 'Window'

ts代码是:

public url = window.URL || window.webkitURL; this.photo = this.url.createObjectURL( res );

bas*_*rat 40

错误TS2339:类型'Window'上不存在属性'webkitURL'

lib.d.ts不附带浏览器特定的内容.但是你可以很容易地做到(window as any).webkitURL.这称为类型断言.

更多

常见的(as any)样式类型断言是由alm提供的quickfix:https://basarat.gitbooks.io/alm/content/features/quickfix.html


Rud*_*lah 7

从TypeScript 2.1.5开始运行的解决方案:

interface Window {
    webkitURL?: any;
}

declare var window: Window;

if (window.webkitURL !== undefined) {
    console.log(window.webkitURL);
}
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,我们声明了Window的接口/形状,可以选择定义webkitURL,然后进行检查以确保已定义。