PrestaShop/PrestaShop
在 GitHub 查看querySelectors are often casted instead of using the TS generic
Open
#27,743 创建于 2022年2月20日
BODeveloper FeatureFront-endGood first issueHacktoberfestImprovementPR available
描述
Prerequisites
- I understand and accept the project's code of conduct
- I have already searched in existing issues and found no previous report of this bug.
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