Review main Merged

86724[TASK] Enable ESLint no-unnecessary-type-assertion

Ayke Halder CR +2 V +2 Normal +107 −107 updated 10/23/26, 14:07

Commit message

The rule `@typescript-eslint/no-unnecessary-type-assertion` reports
type assertions that do not change the type of an expression. They are
removed, so the code is checked against the inferred type. Where an
assertion was the only context a DOM query inferred its element type
from, a type argument takes its place, as introduced with #105417.

Without strictNullChecks the rule also reports every non-null
assertion, so these are removed as well until strictNullChecks is
enabled. The generated JavaScript does not change.

Review

  • Code review CR +2 Done
    • Benjamin Kott +2
    • Benjamin Franzke +1
  • Verified V +2 Done
    • Benjamin Kott +2
    • Benjamin Franzke +1
    • core-ci +1 CI

Issues 3

Files 51

Show 31 more files

Review log 1518

  1. Ayke Halder uploaded patch set 1 10/23/24, 23:31

  2. Ayke Halder updated the commit message in patch set 2 10/23/24, 23:36

  3. core-ci voted on patch set 1 V +1 10/23/24, 23:36 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/68490

  4. core-ci voted on patch set 2 V +1 10/23/24, 23:41 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/68491

  5. Benjamin Franzke commented on patch set 2 CR -2 10/24/24, 02:21

    I like the intention of using no-unnecessary-type-assertions in order to cleanup the code by reducing unneeded `as` statements and better isolating the cases where types could not be infered, but I'm against the used approach: Example in Build/Sources/TypeScript/backend/global-event-handler.ts: The patch replaces `instanceof HTMLElement` with a generic type declaration to HTMLElement (which isn't actually guaranteed!) and thus makes our codebase less robust. `querySelector*` can also return SVGElements for generic selectors, therefore we need to be very cautious to not introduce makes the code less robust. Don't get me wrong, generics are fine in `querySelector*<>`, but only if they are 100% secure and correct (that means: When a tag name is eplicitly mentioned by the selector but the HTMLTagNameMap can not work automatically because additional argument filters are added), other than that they are no way better than a `as` statement. I also would like `strictNullChecks` to be enabled – see 67256: [WIP][TASK] Enable TypeScript strictNullChecks | https://review.typo3.org/c/Packages/TYPO3.CMS/+/67256 – but that's currently still a long way to go, and I don't think we should mangle our codebase to support it, we can never be sure that markup is correct and therefore would need to implemenet hundreds of (in reality) unnecessary checks for possibly broken markup. We need to reduce jquery-style code to webcomponents which makes the codebase more robust. Sorry, I'm not confident this changes improve the code and will therefore block the entire series.
  6. Ayke Halder commented on patch set 2 10/24/24, 13:21

    Reply

    > Example in Build/Sources/TypeScript/backend/global-event-handler.ts: > The patch replaces `instanceof HTMLElement` with a generic type declaration to HTMLElement (which isn't actually guaranteed!) and thus makes our codebase less robust. `querySelector*` can also return SVGElements for generic selectors, therefore we need to be very cautious to not introduce makes the code less robust. Here you have got a point: I just assumed in this case it is save - by knowledge from outside the JS-code - that the object is never an `SVGElement` and always an `HTMLElement`.
  7. Ayke Halder commented on patch set 2 10/24/24, 13:45

    > Don't get me wrong, generics are fine in querySelector*<>, but only if they are 100% secure and correct (that means: When a tag name is eplicitly mentioned by the selector but the HTMLTagNameMap can not work automatically because additional argument filters are added), other than that they are no way better than a as statement. Here my point is that the following code using `as` might unintentionally drop any `null`-possibility - and thereby just covers errors that will uncover on run-time: ``` const someInput: HTMLInputElement = document.querySelector('input[data-foo]') as HTMLInputElement; ``` I prefere one of these: ``` const someInput = document.querySelector<HTMLInputElement>('input[data-foo]'); ``` ``` const someInput: HTMLInputElement|null = document.querySelector<HTMLInputElement>('input[data-foo]'); ``` ``` const someInput: HTMLInputElement = document.querySelector<HTMLInputElement>('input[data-foo]')!; ``` @ben@bnf.dev Please see all the iterations of type-assertions and decide which of these is the best to use. https://www.typescriptlang.org/play/?#code/PQKgsAUJCCA2sAID2AzBAXAFgUwdgHgIYC2ADrNgM4IBG2sSA7ggE5UCus6CAlgHYYcCAMZIyFdPQCevACbY+6HsMLwZAcwXYWhSbIQApAMoik8gFyRIAA1uRRfSt0pjsAST6l23ALwJZSMLsxAroAHQAjuzaUkb02MLoSCwAFADk-F7oaQCUANyQLiEeWQUQRe6e3mEAbqrRZbbWVhAeIoSUuHxIvFXcSDQAVgncPNQoSOx8sgA0CIQC2izJvNRYy4wC-IaEdZTCLDyk-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sND21c1kPNZYjrMlf7IcA0HkDVZj+e42HpO6qAkBxW5TIeV5mHab53p+j5qh+RK2AoCFYVoOhUVZdXIAIEPm3aLgLpGdxnrYN6vr+oG+jYSmobhtwkpHj2RL6LxHcPE8kXwdH1zfkguHoDI4XWGdPyOc5kdm8fFuKuanvc-DsW0-HoOO0zS2u7nAOFUFTB1Fs-BUJsH4jTFqqV+acvZZRtvbBO4Nnbq2Rh7eB79vaKF9vAIB3hVSgMNqqSBQ1H6G01HA2G2DEFfxBtlROf8oZvwzh-dA2cNalVLpqYhMcIHhyljLQ2UJqHp28JnUISClY-yYWrZaXD3YcywWwnB6ACHiz4WLKEZDpZR0NgxMRCCpH0IdnItBCjkba1LgxLRz9KK6OETjK2RjaEmNtsg2RqDk4AM1pgmhqiTpnTzmAlqVs7FhIcYI020CLYsVcYE9xnjGHeKWqnAJEj2GcKsRoliESiZRIrkXcAEAa7uU8vGbyTdqDYQoN3WWoVcDhX7omRBvZ+DCC+H6I+18rzNFKcPUeBhYLcCEuRG4mBCSTK-AuHU8UoHm11kqPgCTMlqLwYgUxKDVYWN8dw0J3VDz5OAYeRx+jnHLNWegSRuCzrSO-iknZPic5+OURk657CNkaPGnwY5hDllnIofww8VybnoA2fchhDMk5pNYWsrOeyUYHKyIDX5RdboG2BXwQFsSWqwhWZtFR8KfZ3K2V4p5-8Xns2oHCj5aiNH4r+eLU50SFlP11tRAltKwUQrJY8mFLsqUhIMZyplCtsWsvIbiwOILCXvJ5aSjxMj+XMPQW7fx4i6VBL9tY5FhDRXopLnqy2nKcWLIKbK7lnzFXJOhaqlOVq1HZLdrqyhBqIDFxFm6llFcgA
  8. Ayke Halder commented on patch set 2 10/16/26, 09:10

    Reply

    No reply: @ben@bnf.dev please remove your double down-vote here.
  9. Benjamin Franzke commented on patch set 2 CR -1 10/16/26, 12:32

    Reply

    you said in the other comment that "Here you have got a point", so I guess that needs to be adapted. anyway, I'll change to -1 so you're not blocked once reworked.
  10. Benjamin Franzke commented on patch set 2 10/23/26, 12:26

    Reply

    Preferred one is: ``` const someInput = document.querySelector<HTMLInputElement>('input[data-foo]'); ``` Reason: we want to write as less types as possible and takeover infered types as much as possible. (=best TypeScript)

    Reply

    Right, therefore I suggest to avoid adding a type qualifier, if we don't know it for sure. That means a case like ``` let item = form.querySelector('[name=' + CSS.escape(name) + ']'); ``` Should just stay as-is. Reopening in order for Benji (who will rebase this patch) to consider this.
  11. Benjamin Kott uploaded patch set 3 CR -1 V +1 10/23/26, 12:48

  12. core-ci voted on patch set 3 V +1 10/23/26, 12:59 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/112301

  13. Benjamin Franzke commented on patch set 3 CR +1 V +1 10/23/26, 13:21

    Reply

    Done

    Reply

    Done
  14. Benjamin Kott voted on patch set 3 CR +2 V +2 10/23/26, 13:21

  15. Ayke Halder commented on patch set 3 CR -1 10/23/26, 13:22

    Got one hit via regex to change: Build/Sources/TypeScript/backend/element/combobox-element.ts Line 475: ``` return assignedElements.find(el => el.tagName === 'INPUT') as HTMLInputElement | null; ``` Should be ``` return assignedElements.find<HTMLInputElement>(el => el.tagName === 'INPUT'); ```
  16. Ayke Halder commented on patch set 3 CR 0 10/23/26, 13:34

    Reply

    Looks like this component might have more issues as `this.renderRoot` (and neither `this.shadowRoot`) might not exist in setup of `LitELement` life-cycle. So this might be another issue to solve.
  17. Benjamin Kott merged patch set 4 10/23/26, 13:58

  18. Ayke Halder commented on patch set 3 10/23/26, 14:07

    Reply Unresolved

    `this.shadowRoot` did not have any issue here. My local environment had all build sources cleaned - and that issued an error - solved by: `Build/Scripts/runTests.sh -s build` ---- The issue: Here `find()` returns either an `HTMLInputElement` or `undefined` - that is not `null`. The return type of `getInput()` suggests `HTMLInputElement|null`. Actually the curent return values are `HTMLInputElement|null|undefined`. Solvable via 'Type Guard' - ̀is HTMLInputElement`: Option A: ``` return assignedElements.find((el): el is HTMLInputElement => el.tagName === 'INPUT'); // returns HTMLInputElement|undefined ``` Option B: ``` return assignedElements.find((el): el is HTMLInputElement => el.tagName === 'INPUT') ?? null; // returns HTMLInputElement|null ``` To not change the already defined return value of `getInput()`, select the second solution.