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

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

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

### TL;DR

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

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

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

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

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

It can be done like this,

![error while accessing the id property in the h1 HTML element tag](https://melvingeorge.me/_next/static/images/h1tag-id-error-c130a8db6abd472a3d4b7f0033c89453.png)

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

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

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

It can be done like this,

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

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

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

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

That's all 😃!

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

---