How to get or access the properties and methods of meta HTML element tags without errors in TypeScript?
Tags: typescript
Published: 2021-09-27 00:00:00

---
  title: How to get or access the properties and methods of meta HTML element tags without errors in TypeScript?
  published: true
  tags: TypeScript
  date: Mon Sep 27 2021 05:30:00 GMT+0530 (India Standard Time)
  canonical_url: https://melvingeorge.me/blog/get-or-access-properties-methods-meta-html-element-tag-without-errors-typescript
  cover_image: https://melvingeorge.me/_next/static/images/main-e817a48af263d889d162a069a94a8ed7.jpg
  ---
  [Originally posted here!](https://melvingeorge.me/blog/get-or-access-properties-methods-meta-html-element-tag-without-errors-typescript)
  
To get or access the properties and methods of the `meta` HTML element tags without having errors or red squiggly lines in TypeScript, you have to assert the type for the `meta` HTML element tag using the `HTMLMetaElement` interface in TypeScript.

The errors or warnings are shown by the TypeScript compiler since the compiler doesn't know more information on the `meta` HTML element tag ahead of time. But we know that we are referencing the `meta` HTML tag and thus we can tell the compiler to use the `HTMLMetaElement` interface which contains the declarations for properties and methods.

### TL;DR

```ts
// get reference to the first 'meta'
// HTML element tag in the document
// with type assertion using the HTMLMetaElement interface
const metaTag = document.querySelector("meta") as HTMLMetaElement;

// no errors or red squiggly lines will be
// shown while accessing properties or methods
// since we have asserted the type
// for the 'meta' HTML element tag 😍
const content = metaTag.content;
```

To understand it better let's say we are selecting the first `meta` HTML element tag using the `document.querySelector()` method like this,

```ts
// get reference to the first 'meta'
// HTML element tag in the document
const metaTag = document.querySelector("meta");
```

Now let's try to get the value of a property called `content` which is used in the `meta` HTML element tag as an attribute.

It can be done like this,

![error while accessing the content property in the meta HTML element tag](https://melvingeorge.me/_next/static/images/metatag-content-error-a6b6e52c899c30cc60477758ac740f95.png)

As you can see that the TypeScript compiler is showing a red squiggly line below the `metaTag` object. If you hover over the `metaTag` you can see an error saying `Object is possibly 'null'`. This is because TypeScript is not sure whether the `metaTag` object will have properties and methods ahead of time.

Now to avoid this error we can assert the type for the `meta` HTML element tag using the `HTMLMetaElement` interface in TypeScript.

To do Type assertion we can use the `as` keyword after the `document.querySelector("meta")` method followed by writing the interface name `HTMLMetaElement`.

It can be done like this,

```ts
// get reference to the first 'meta'
// HTML element tag in the document
// with type assertion using the HTMLMetaElement interface
const metaTag = document.querySelector("meta") as HTMLMetaElement;

// no errors or red squiggly lines will be
// shown while accessing properties or methods
// since we have asserted the type
// for the 'meta' HTML element tag 😍
const content = metaTag.content;
```

Now if hover over the `content` property in the `metaTag` object TypeScript shows more information about the property itself which is again cool and increases developer productivity and clarity while coding.

![more information about the content property in the meta HTML element tag](https://melvingeorge.me/_next/static/images/more-info-content-property-meta-tag-aef8bd9b17278e7f8c5db92eaff6dd93.png)

See the above code live in [codesandbox](https://codesandbox.io/s/get-or-access-properties-and-method-of-meta-html-element-tag-without-errors-in-typescript-rdul5?file=/src/index.ts).

That's all 😃!

### Feel free to share if you found this useful 😃.

---