Issue Task Resolved

#105417 ESLint: Prepare for `@typescript-eslint/no-unnecessary-type-assertion` rule

Ayke Halder 10/23/24, 15:50 updated 10/23/26, 12:30

Description

There are some type assertions that cover up that a value might be null on run-time as actually no element is found.

Current implementation

this.clearableElements = document.querySelectorAll('.t3js-clearable') as NodeListOf<HTMLInputElement>;

const targetElement = parentElement.querySelector('button[data-bits="' + bits + '"]') as HTMLButtonElement;

<HTMLInputElement>document.querySelector('input[name="' + e.data.fieldName + '"]');

——

Implementation with null

IMO the type assertion should not remove the null possibility – and could look like this:

this.clearableElements = document.querySelectorAll('.t3js-clearable') as NodeListOf<HTMLInputElement|null>;

const targetElement = parentElement.querySelector('button[data-bits="' + bits + '"]') as HTMLButtonElement|null;

<HTMLInputElement|null>document.querySelector('input[name="' + e.data.fieldName + '"]');

——

Improved implementation with null

To further improve use the following type assertion for find/@closest@/@querySelector@/@querySelectorAll@, as this will give the proper type including the null-possibility:

this.clearableElements = document.querySelectorAll<HTMLInputElement>('.t3js-clearable');

const targetElement = parentElement.querySelector<HTMLButtonElement>('button[data-bits="' + bits + '"]');

const field = document.querySelector<HTMLInputElement>('input[name="' + e.data.fieldName + '"]');

Reviews 4

History 211

  1. Ayke Halder commented and changed status Under Review Resolved done 0 % 100 % <span data-processor="relativetime" data-value="2026-10-23T12:30:16+00:00" > 10/23/26, 12:30 </span>

    Applied in changeset commit:01752b13fbe8f1616576ae1ca71a5da6d139a0ba.