typeScript返回HTML模板元素,来自构造函数不起作用
创建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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尽管不是理想的,但我能够通过实现以下接口来保持打字稿编译器的快乐:
然后使用该接口:
注意:
为什么我不使用
customElements.define(< tag-name>,template)
?我不打算创建
htmltemplatelement
的自定义实例,我只想使用其他实用程序扩展方法返回现有htmltemplatelement
。而且,我的方法很可能完全是错误的。
但是,这与这里提出的问题不同。
Although not ideal, I was able to keep the typescript compiler happy by implementing the following interface:
and then using the interface:
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 existingHTMLTemplateElement
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.