Skip to main content

Command Palette

Search for a command to run...

Handling DOM Elements and Type Assertions in TypeScript

Published
2 min read
M

As a former 3D Animator with more than 12 years of experience, I have always been fascinated by the intersection of technology and creativity. That's why I recently shifted my career towards MERN stack development and software engineering, where I have been serving since 2021.

With my background in 3D animation, I bring a unique perspective to software development, combining creativity and technical expertise to build innovative and visually engaging applications. I have a passion for learning and staying up-to-date with the latest technologies and best practices, and I enjoy collaborating with cross-functional teams to solve complex problems and create seamless user experiences.

In my current role as a MERN stack developer, I have been responsible for developing and implementing web applications using MongoDB, Express, React, and Node.js. I have also gained experience in Agile development methodologies, version control with Git, and cloud-based deployment using platforms like Heroku and AWS.

I am committed to delivering high-quality work that meets the needs of both clients and end-users, and I am always seeking new challenges and opportunities to grow both personally and professionally.

Fetching DOM Elements with TypeScript

When fetching data through the DOM in TypeScript, you might encounter warnings that the element could be null or undefined. Let's explore this with an example.

Example Code:

const button = document.querySelector('button');
const input1 = document.getElementById("num1");
const input2 = document.getElementById("num2");

function add(num1, num2) {
   return num1 + num2;
}

button.addEventListener('click', () => {
    console.log(input1.value, input2.value);
});

In the code above, TypeScript will show warnings on .value because the elements might be null.

Fixing the Warning:

To fix this, you can use type assertions to tell TypeScript that these elements are not null and are of a specific type.

const input1 = document.getElementById("num1")! as HTMLInputElement;
const input2 = document.getElementById("num2")! as HTMLInputElement;

Adding ! asserts that the element is not null, and as HTMLInputElement specifies the type of the element.

Handling Types:

If you hover over num1, you'll see its type is given as any. To fix the type, define the function with specific types:

function add(num1: number, num2: number): number {
   return num1 + num2;
}

However, this will show an error because the input.value is a string. To resolve this, you need to convert the string to a number:

button.addEventListener('click', () => {
    console.log(+input1.value, +input2.value);
});

Using + before input1.value and input2.value converts the strings to numbers.

TypeScript Overview

TypeScript enhances JavaScript by adding several powerful features:

Key Features of TypeScript:

  1. Types: Adds static types, helping to catch errors early.

  2. Next-gen JavaScript Features: Compiles down for older browsers, ensuring compatibility.

  3. Non-JavaScript Features: Introduces interfaces and generics for better code structuring.

  4. Meta-Programming Features: Supports decorators for advanced programming techniques.

  5. Rich Configuration Options: Offers extensive configuration for various project needs.

  6. Modern Tooling: Enhances the development experience even in non-TypeScript projects.