Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prefer-dom-node-append: Property 'append' does not exist on type 'ChildNode'. #2474

Open
Samuel-Therrien-Beslogic opened this issue Oct 8, 2024 · 1 comment
Labels
docs evaluating help wanted types Issues that happen in TypeScript or that require types

Comments

@Samuel-Therrien-Beslogic
Copy link

Samuel-Therrien-Beslogic commented Oct 8, 2024

Description

Code using appendChild on a Node should not be autofixed to use append. I think this can be improved by using type information and only raising an issue when (or at the very least not autofixing unless) it is known to be an Element.

This relates to #347

Fail

These are currently working fine and should still work

const foo = (element: Element, htmlElement: HTMLElement) => {
  element.appendChild(node)
  htmlElement.appendChild(node)
}
document.getElementsByName("name")[0]?.appendChild(document.createTextNode("foo"))

Pass

const foo = (node: Node, childNode: ChildNode, untyped,  element: Element) => {
  node.appendChild(node) // Current false positive
  childNode.appendChild(node) // Current false positive
  childNode.appendChild(node) // Current false positive
  element.append(node)
}
document.body.childNodes[0]?.appendChild(document.createTextNode("foo")) // Current false positive

Additional Info

These examples are oversimplified just to get the right type.

@sindresorhus sindresorhus changed the title rule-name: Reduce dangerous false-positives with type information Reduce dangerous false-positives with type information Oct 8, 2024
@fregante fregante changed the title Reduce dangerous false-positives with type information prefer-modern-dom-apis: Property 'append' does not exist on type 'ChildNode'. Oct 9, 2024
@fregante fregante added the types Issues that happen in TypeScript or that require types label Oct 9, 2024
@fregante
Copy link
Collaborator

fregante commented Oct 9, 2024

A simplified example:

document.body.firstChild!.appendChild(new Text('a'));
// ESLint Error: Prefer `Node#append()` over `Node#appendChild()`.

and then:

document.body.firstChild!.append(new Text('a'));
// TypeScript Error: Property 'append' does not exist on type 'ChildNode'.

So yeah it is true that appendChild works where append doesn't, but that actually is a footgun. Try this perfectly valid TypeScript code:

new Text().appendChild(new Text());
// HierarchyRequestError: The operation would yield an incorrect node tree.

That's a runtime error.

prefer-dom-node-append is technically wrong for suggesting append where it doesn't exist, but if you follow the advice fully it will save you from runtime errors, which means the rule is still valid as is.

Perhaps what I'd suggest is expanding the error/docs to include this piece of information and to fix the error (no such thing as Node#append, it's Element#append)

@fregante fregante changed the title prefer-modern-dom-apis: Property 'append' does not exist on type 'ChildNode'. prefer-dom-node-append: Property 'append' does not exist on type 'ChildNode'. Oct 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs evaluating help wanted types Issues that happen in TypeScript or that require types
Projects
None yet
Development

No branches or pull requests

2 participants