typeScript返回HTML模板元素,来自构造函数不起作用

发布于 2025-02-13 11:14:51 字数 1353 浏览 1 评论 0原文

创建template的实例时,使用new是“非法”,其中template扩展了htmltemplatelement

为了克服此限制,我从template> template sonstructor frol document.getElementById(id) sonstructor for document.getElementById(id)使用> htmltemplatelement

export class Template {
    private htmlTemplateElement: HTMLTemplateElement;
    constructor(id: string) {
        this.htmlTemplateElement = document.getElementById(id) as HTMLTemplateElement;
        return Object.assign(this.htmlTemplateElement, this) 
    }

    public test = () => this.htmlTemplateElement.innerHTML
}

。 > HTML模板元素存在于DOM中,
我可以创建模板的新实例,并使用扩展方法test()如下所示:

const template = new Template(id)
console.log(template.test())
console.log(template.innerHTML) 

这两个console.log()正常工作并将正确的文本打印到控制台。

但是,打字稿编译器抱怨template.innerhtml
我遇到的错误,说innerhtml在类型模板

问题 上不存在?

我尝试使用导出类模板扩展了htmltemplatelement
这是不起作用的,因为使用new创建一个实例是

我喜欢打字稿,但有时会遇到类型的检查。 请帮我在这里。

It is 'illegal' to use new when creating an instance of Template, where Template extends an HTMLTemplateElement.

To overcome this limitation, I get and return an HTMLTemplateElement using document.getElementById(id) from the Template constructor as below:

export class Template {
    private htmlTemplateElement: HTMLTemplateElement;
    constructor(id: string) {
        this.htmlTemplateElement = document.getElementById(id) as HTMLTemplateElement;
        return Object.assign(this.htmlTemplateElement, this) 
    }

    public test = () => this.htmlTemplateElement.innerHTML
}

Providing an HTML Template Element exist in the DOM,
I can create a new instance of Template and use the extension method test() as illustrated below:

const template = new Template(id)
console.log(template.test())
console.log(template.innerHTML) 

Both console.log() works just fine and prints the correct text to the console.

HOWEVER, the typescript compiler complains about template.innerHTML.
The error I get, saying innerHTML does not exist on type Template

Question: How can I add type information so I do not get a compiler error?

I have tried to use export class Template extends HTMLTemplateElement.
That does not work since it is illegal to create an instance using new.

I love typescript, but sometimes the type checking gets in my way.
Help me out here, please.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

大海や 2025-02-20 11:14:51

尽管不是理想的,但我能够通过实现以下接口来保持打字稿编译器的快乐:

export interface ITemplate {
    [key:string]: any;
    test(): string;
}

然后使用该接口:

export Template implements ITemplate {
...
}

注意:
为什么我不使用customElements.define(< tag-name>,template)
我不打算创建 htmltemplatelement的自定义实例,我只想使用其他实用程序扩展方法返回现有htmltemplatelement

而且,我的方法很可能完全是错误的。
但是,这与这里提出的问题不同。

Although not ideal, I was able to keep the typescript compiler happy by implementing the following interface:

export interface ITemplate {
    [key:string]: any;
    test(): string;
}

and then using the interface:

export Template implements ITemplate {
...
}

Note:
Why did I not use customElements.define(<tag-name>,Template)?
I do not intend to create a new custom instance of HTMLTemplateElement, I just want to return an existing HTMLTemplateElement with additional utility extension methods.

Also, It might very well be possible that my approach is completely wrong.
However, that is a different topic than the question asked here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文