Skip to content

Commit

Permalink
Narrows type of parent in render functions. (#3863)
Browse files Browse the repository at this point in the history
* Narrows type of `parent` in render functions.

https://gist.github.com/developit/f4c67a2ede71dc2fab7f357f39cff28c is the recommended way to partial root rendering in Preact 10+, but it isn't API compliant code given the current API definition as expressed by the TS types.
This PR changes the type of `parent` to align with what is *actually* required by the preact render functions according to @developit.

Assuming the linked code actually works, then it means preact doesn't actually need a NodeList, but rather just needs an `ArrayLike<Node>`.
Along with limiting the property set of `Parent`, I have also narrowed `Parent['childNodes']` to be an `ArrayLike<Node>` so someone can assign a `Node[]` to it.

I suspect my stylistic choice of a single long line is not acceptable for this repository, but I'm not clear what the proper way to break this into multiple lines is for this repository.  If someone can provide me with feedback on how to correctly split the lines, please let me know!

Note: I recognize that this is a pretty substantial type change, but without it the official recommendation (and only option in Preact 11) for doing partial root rendering is incorrect.
Either the recommendation for how to do partial root rendering should be corrected to not lead people to an incorrect solution, or this PR (or one like it) should be merged to correctly express the type that preact expects for its `render` functions.

* Update index.d.ts

* Update src/index.d.ts

Co-authored-by: Andre Wiggins <[email protected]>

* Update preact.tsx

---------

Co-authored-by: Andre Wiggins <[email protected]>
  • Loading branch information
MicahZoltu and andrewiggins authored Jan 27, 2023
1 parent c483d96 commit 15913ad
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,19 @@ export namespace h {
// Preact render
// -----------------------------------

interface ContainerNode {
nodeType: Node['nodeType'];
parentNode: Node['parentNode'];
firstChild: Node['firstChild'];
insertBefore: Node['insertBefore'];
appendChild: Node['appendChild'];
removeChild: Node['removeChild'];
childNodes: ArrayLike<Node>
}

export function render(
vnode: ComponentChild,
parent: Element | Document | ShadowRoot | DocumentFragment
parent: ContainerNode
): void;
/**
* @deprecated Will be removed in v11.
Expand All @@ -286,12 +296,12 @@ export function render(
*/
export function render(
vnode: ComponentChild,
parent: Element | Document | ShadowRoot | DocumentFragment,
parent: ContainerNode,
replaceNode?: Element | Text
): void;
export function hydrate(
vnode: ComponentChild,
parent: Element | Document | ShadowRoot | DocumentFragment
parent: ContainerNode
): void;
export function cloneElement(
vnode: VNode<any>,
Expand Down
6 changes: 6 additions & 0 deletions test/ts/preact.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ render(
document
);

// Mounting into different types of Nodes
render(h('div', {}), document.createElement('div'));
render(h('div', {}), document);
render(h('div', {}), document.createElement('div').shadowRoot!);
render(h('div', {}), document.createDocumentFragment());

// Accessing children
const ComponentWithChildren: FunctionalComponent<DummerComponentProps> = ({
input,
Expand Down

0 comments on commit 15913ad

Please sign in to comment.