admin管理员组文章数量:1203458
I get error when I try to get the content of meta tag:
document.getElementById('mymetatag').content
in Javascript is works, but typescript say that
Property 'content' does not exist on type 'HTMLElement'
So how to extend the HTMLElement type interface to support content property?
I get error when I try to get the content of meta tag:
document.getElementById('mymetatag').content
in Javascript is works, but typescript say that
Property 'content' does not exist on type 'HTMLElement'
So how to extend the HTMLElement type interface to support content property?
Share Improve this question edited Jun 18, 2020 at 13:20 julianstark999 3,6161 gold badge29 silver badges42 bronze badges asked Jun 18, 2020 at 13:18 Jon SudJon Sud 11.6k31 gold badges102 silver badges226 bronze badges 3 |5 Answers
Reset to default 13TypeScript doesn't know what kind of element has this specific ID. It cannot trust the element even exists to begin with!
You need to introduce another check in order to validate your assumption:
const element: HTMLElement | null = document.getElementById('mymetatag');
if (element instanceof HTMLMetaElement) {
element.content;
}
Update
You can also use assertion signatures.
function assert(condition: boolean, message: string): asserts condition {
if (!condition) {
throw new Error(message);
}
}
Then your solution becomes:
const element: HTMLElement | null = document.getElementById('mymetatag');
assert(element instanceof HTMLMetaElement, "A <meta> element of id 'mymetatag' needs to exist in the DOM.");
element.content; // string
Also, for <template id="source">
, you can use HTMLTemplateElement
const source = document.querySelector('#source') as HTMLTemplateElement;
const sourceClone = source.content.cloneNode(true) as HTMLElement;
document.getElementById()
returns an HTMLElement
.
But you know that your result is a meta tag, which is a type of HTMLMetaElement
.
Looking at the docs you can see that HTMLElement
is the ancestor of HTMLMetaElement
so you should downcast your element to this type. You can do this with two different syntaxes:
const metaElement = document.getElementById("mymetatag") as HTMLMetaElement;
// Or the second version:
//const metaElement = <HTMLMetaElement>document.getElementById("mymetatag");
const metaContent = metaElement.content;
See more about type assertions here.
Try following:
(<HTMLMetaElement> document.getElementById("mymetatag")).content;
Can use getAttribute method:
document.getElementById('mymetatag').getAttribute('content')
本文标签: javascriptProperty 39content39 does not exist on type 39HTMLElement39 in TypescriptStack Overflow
版权声明:本文标题:javascript - Property 'content' does not exist on type 'HTMLElement' in Typescript - Stack Overf 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738665912a2105698.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
(<HTMLInputElement>document.getElementById("mymetatag")).content
?? This works only if the element is input.. – Maniraj Murugan Commented Jun 18, 2020 at 13:22Property 'content' does not exist on type 'HTMLInputElement'
. also I prefer to extend the interface – Jon Sud Commented Jun 18, 2020 at 13:23mymetatag
is mentioned? – Maniraj Murugan Commented Jun 18, 2020 at 13:24