PrestaShop/PrestaShop

querySelectors are often casted instead of using the TS generic

Open

#27,743 创建于 2022年2月20日

在 GitHub 查看
 (0 评论) (1 反应) (0 负责人)PHP (7,621 star) (4,734 fork)batch import
BODeveloper FeatureFront-endGood first issueHacktoberfestImprovementPR available

描述

Prerequisites

Describe the bug and add screenshots

https://github.com/PrestaShop/PrestaShop/search?l=TypeScript&p=2&q=querySelector

There are a lot of querySelectors casted, but querySelector has a generic which can be used, in order to get the element type we want

Casting it is short-circuiting the TS typecheck, and it might lead to unwanted undefined elements

Expected behavior

Instead of let test = document.querySelector('.test') as HTMLElement, we should use let test = document.querySelector<HTMLElement>('.test');

And then verify using Optional Chaining or a simple condition if the element exisit

It will ensure that every elements exist before using it and enforce our codebase, limit bugs...

Steps to reproduce

No bug

PrestaShop version(s) where the bug happened

8.x

PHP version(s) where the bug happened

No response

If your bug is related to a module, specify its name and its version

No response

贡献者指南

querySelectors are often casted instead of using the TS generic · PrestaShop/PrestaShop#27743 | Good First Issue