Return cloneNode as the receiver type#2484
Conversation
|
Thanks for the PR! This section of the codebase is owned by @saschanaz - if they write a comment saying "LGTM" then it will be merged. |
|
@microsoft-github-policy-service agree |
|
This was rejected in the past |
Thanks for linking that. I dug through #302, microsoft/TypeScript#17818, #811, and #842 to make sure I understood the previous decision. My read is that the old rejection was about compiler/type-checking cost from using
So I agree this is the same area as the older rejected attempts, but I think the current compiler behavior may be worth re-evaluating rather than treating #302 as a direct blocker. |
Summary
I updated the DOM override for
Node.cloneNodeso the generated DOM libs return the receiver type instead of the baseNodetype. This keeps existingNodebehavior asNode, while preserving exact static types for DOM subtypes and custom element subclasses.Addresses microsoft/TypeScript#283.
Why
cloneNodereturns a clone of the node it is called on, but the current declaration forces callers to cast fromNodeeven when the receiver has a more specific static type.I also checked the prior
cloneNodeoverload history before making this change. This patch uses one receiver-type return onNoderather than adding per-element overloads.Validation
npm run testnpx tsc baselines/dom.generated.d.ts <@types/jquery> <@types/tablesorter> --target es2020 --lib es2020 --strict --noEmit --skipLibCheck false --typeRoots <scratch @types>npx tsc baselines/dom.generated.d.ts --lib es5 --noEmit --diagnosticsDiagnostics comparison from this branch: