From cc4998117a467c606d2a7d0ca5a6e7154cafac95 Mon Sep 17 00:00:00 2001 From: David Arenas Date: Thu, 16 May 2024 21:50:19 +0200 Subject: [PATCH] Interactivity API: Move init.js to TypeScript (#61723) * Move init.js to typescript * Throw an error for regions without parent Co-authored-by: DAreRodz Co-authored-by: sirreal Co-authored-by: michalczaplinski --- packages/interactivity/src/{init.js => init.ts} | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) rename packages/interactivity/src/{init.js => init.ts} (77%) diff --git a/packages/interactivity/src/init.js b/packages/interactivity/src/init.ts similarity index 77% rename from packages/interactivity/src/init.js rename to packages/interactivity/src/init.ts index fb510a9c00fced..78ba6bd23ee79e 100644 --- a/packages/interactivity/src/init.js +++ b/packages/interactivity/src/init.ts @@ -1,7 +1,7 @@ /** * External dependencies */ -import { hydrate } from 'preact'; +import { hydrate, type ContainerNode, type ComponentChild } from 'preact'; /** * Internal dependencies */ @@ -11,7 +11,10 @@ import { directivePrefix } from './constants'; // Keep the same root fragment for each interactive region node. const regionRootFragments = new WeakMap(); -export const getRegionRootFragment = ( region ) => { +export const getRegionRootFragment = ( region: Element ): ContainerNode => { + if ( ! region.parentElement ) { + throw Error( 'The passed region should be an element with a parent.' ); + } if ( ! regionRootFragments.has( region ) ) { regionRootFragments.set( region, @@ -29,7 +32,7 @@ function yieldToMain() { } // Initial vDOM regions associated with its DOM element. -export const initialVdom = new WeakMap(); +export const initialVdom = new WeakMap< Element, ComponentChild[] >(); // Initialize the router with the initial DOM. export const init = async () => {