新的 TypeScript 版本不包括“window.navigator.msSaveBlob”

问题描述 投票:0回答:3

我有一个 TypeScript 项目(https://github.com/jmaister/excellentexport)并且它工作正常。

添加dependabot进程后,建议升级typescript:

Bump typescript from 4.3.4 to 4.4.3

但是,由于我正在维护的库引用了 Internet Explorer 到旧 Internet Explorer 属性,因此无法使用新版本进行构建。

这是构建错误的示例:

src/excellentexport.ts:143:30 - error TS2339: Property 'msSaveBlob' does not exist on type 'Navigator'.
143         if (window.navigator.msSaveBlob) {
                                 ~~~~~~~~~~
src/excellentexport.ts:145:30 - error TS2339: Property 'msSaveBlob' does not exist on type 'Navigator'.
145             window.navigator.msSaveBlob(blob, filename);
                                 ~~~~~~~~~~
src/excellentexport.ts:278:34 - error TS2339: Property 'msSaveBlob' does not exist on type 'Navigator'.

我应该删除对旧版 Internet Explorer 的支持吗?有办法继续使用那些 IE 特定属性吗?

typescript dependencies
3个回答
39
投票

我最近遇到了完全相同的问题,我找到的解决方案是扩展

Navigator
命名空间中的
global
接口,因此它仍然包含
msSaveBlob
,基于 TypeScript 在这里记录
msSaveBlob
的方式: MSFileSaver

这是我使用的代码:

declare global {
    interface Navigator {
        msSaveBlob?: (blob: any, defaultName?: string) => boolean
    }
}

if (navigator.msSaveBlob) {
    // use navigator.msSaveBlob
}

3
投票

我在我的 appmodule.ts 文件中声明了全局命名空间,因为它在像这样的多个文件中使用

 declare global{
 interface Navigator{
    msSaveBlob:(blob: Blob,fileName:string) => boolean
    }
 }

调用文件看起来像这样**//它抛出了像你提到的构建错误,

if (navigator.msSaveBlob) {
   // IE 10+
   navigator.msSaveBlob(blob, fileName);
}

0
投票
  if ((window.navigator as any).msSaveBlob) {
        (window.navigator as any).msSaveBlob(blob, filename);
      }
© www.soinside.com 2019 - 2024. All rights reserved.