PrestaShop/PrestaShop

querySelectors are often casted instead of using the TS generic

开放

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

 (0 条评论) (1 个反应) (0 位负责人)PHP (4,734 个派生)batch import
BODeveloper FeatureFront-endGood first issueHacktoberfestImprovementPR available

仓库指标

星标
 (7,621 个星标)
PR 合并指标
 (PR 指标待抓取)

描述

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

贡献者指南