Project

General

Profile

Actions

Task #105417

open

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

Added by Ayke Halder about 1 month ago. Updated about 1 month ago.

Status:
Under Review
Priority:
Should have
Assignee:
-
Category:
Backend JavaScript
Target version:
-
Start date:
2024-10-23
Due date:
% Done:

0%

Estimated time:
TYPO3 Version:
14
PHP Version:
Tags:
Complexity:
Sprint Focus:

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 + '"]');

Related issues 1 (1 open0 closed)

Related to TYPO3 Core - Task #105423: ESLint: Implement `@typescript-eslint/no-unnecessary-type-assertion` ruleUnder Review2024-10-23

Actions
Actions #1

Updated by Gerrit Code Review about 1 month ago

  • Status changed from New to Under Review

Patch set 1 for branch main of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/c/Packages/TYPO3.CMS/+/86716

Actions #2

Updated by Ayke Halder about 1 month ago

  • Related to Task #105423: ESLint: Implement `@typescript-eslint/no-unnecessary-type-assertion` rule added
Actions #3

Updated by Gerrit Code Review about 1 month ago

Patch set 2 for branch main of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/c/Packages/TYPO3.CMS/+/86716

Actions

Also available in: Atom PDF