gpt4 book ai didi

javascript - typescript :使用预先存在的标识符覆盖/扩展 Lib.d.ts

转载 作者:行者123 更新时间:2023-11-30 15:57:02 25 4
gpt4 key购买 nike

我需要获取 typescript 才能停止提示我的代码。它在浏览器中运行良好,但全屏 api 还不是官方的,因此 typescript 定义不是最新的。

我正在调用 document.documentElement.msRequestFullscreen。这会导致类型错误:

Property 'msRequestFullscreen' does not exist on type 'HTMLElement'.

查看 lib.d.ts 后,我发现:

documentElement: HTMLElement;

因此 documentElement 被设置为 HTMLElement 类型。我尝试添加自定义定义来覆盖 documentElement。我的自定义定义:

// Extend Document Typings
interface Document {
msExitFullscreen: any;
mozCancelFullScreen: any;
documentElement: {
msRequestFullscreen: any;
mozRequestFullScreen: any;
}
}

我尝试扩展 Document 的接口(interface),但出现错误

错误是:

lib.d.ts:5704:5 
Duplicate identifier 'documentElement'.

我的 typescript 课

export class ToggleFullScreen {
viewFullScreenTriggerID: string;
viewFullScreenClass: string;
cancelFullScreenClass: string;
viewFullscreenElem: any;
activeIcon: string;
notFullscreenIcon: string;
isFullscreenIcon: string


constructor() {
this.viewFullScreenTriggerID = "#fullScreenTrigger";
this.viewFullScreenClass = "not-fullscreen";
this.cancelFullScreenClass = "is-fullscreen";
this.notFullscreenIcon = "/assets/icon/fullscreen-enter.svg";
this.isFullscreenIcon = "/assets/icon/fullscreen-exit.svg";
this.activeIcon = this.notFullscreenIcon;
}

toggleFullScreen() {
this.viewFullscreenElem = document.querySelector(this.viewFullScreenTriggerID);

if (this.viewFullscreenElem.classList.contains(this.viewFullScreenClass)) {

var docElm = document.documentElement;
if (docElm.requestFullscreen) {
docElm.requestFullscreen();
} else if (docElm.msRequestFullscreen) {
docElm.msRequestFullscreen();
} else if (docElm.mozRequestFullScreen) {
docElm.mozRequestFullScreen();
} else if (docElm.webkitRequestFullScreen) {
docElm.webkitRequestFullScreen();
}

this.viewFullscreenElem.classList.toggle(this.viewFullScreenClass);
this.viewFullscreenElem.classList.toggle(this.cancelFullScreenClass);
this.activeIcon = this.isFullscreenIcon;

}

else if (this.viewFullscreenElem.classList.contains(this.cancelFullScreenClass)) {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.msExitFullscreen) {
document.msExitFullscreen();
} else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
} else if (document.webkitCancelFullScreen) {
document.webkitCancelFullScreen();
}

this.viewFullscreenElem.classList.toggle(this.viewFullScreenClass);
this.viewFullscreenElem.classList.toggle(this.cancelFullScreenClass);
this.activeIcon = this.notFullscreenIcon;
}
}
}

停止 typescript 编译错误的正确方法是什么?

更新:我找到了解决方法。我没有尝试覆盖设置为 HTMLElement 类型的 documentElement,而是扩展了 HTMLElement 并添加了缺少的属性。

// Extend Document Typings
interface Document {
msExitFullscreen: any;
mozCancelFullScreen: any;
}


interface HTMLElement {
msRequestFullscreen(): void;
mozRequestFullScreen(): void;
}

最佳答案

我正在使用较新版本的 Typescript,但遇到了同样的问题。

我尝试了上面的解决方案但它没有用 - 似乎我在屏蔽现有界面。

为了能够正确地扩展这两个接口(interface),我不得不使用声明全局:

declare global {
interface Document {
msExitFullscreen: any;
mozCancelFullScreen: any;
}


interface HTMLElement {
msRequestFullscreen: any;
mozRequestFullScreen: any;
}
}

这样一来,我就能够正确地编译和使用如下代码:

fullScreenClick(e): any {
var element = document.documentElement;
if (!$('body').hasClass("full-screen")) {
$('body').addClass("full-screen");
$('#fullscreen-toggler').addClass("active");
if (element.requestFullscreen) {
element.requestFullscreen();
} else if (element.mozRequestFullScreen()) {
element.mozRequestFullScreen();
} else if (element.webkitRequestFullscreen) {
element.webkitRequestFullscreen();
} else if (element.msRequestFullscreen) {
element.msRequestFullscreen();
}
} else {
$('body').removeClass("full-screen");
$('#fullscreen-toggler').removeClass("active");
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
} else if (document.webkitExitFullscreen) {
document.webkitExitFullscreen();
}
}
}

关于javascript - typescript :使用预先存在的标识符覆盖/扩展 Lib.d.ts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38361940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com