Skip to content
Advertisement

Property ‘content’ does not exist on type ‘HTMLElement’ in Typescript

I get error when I try to get the content of meta tag:

JavaScript

in Javascript is works, but typescript say that

JavaScript

So how to extend the HTMLElement type interface to support content property?

Advertisement

Answer

TypeScript 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:

JavaScript

Update

You can also use assertion signatures.

JavaScript

Then your solution becomes:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement