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

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

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

### TL;DR

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

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

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

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

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

It can be done like this,

![error while accessing the id property in the figure HTML element tag](https://melvingeorge.me/_next/static/images/figuretag-id-error-6b28e13b13cb3b5acab09b4b61c9e7eb.png)

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

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

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

It can be done like this,

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

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

Now if hover over the `id` property in the `figureTag` 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 id property in the figure HTML element tag](https://melvingeorge.me/_next/static/images/more-info-id-property-figure-tag-149b6ee3581b233e9db477e57e7dc90a.png)

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

That's all 😃!

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

---