Skip to content

Commit 6173e1a

Browse files
fix(types): make framework declarations resolve under node16/nodenext
The framework .d.ts files imported the core types as "../index", which TypeScript rejects for ESM packages under moduleResolution node16/nodenext (TS2835). Rewrite them to "../index.js", which resolves to index.d.ts in every resolution mode. Also fix the Preact hook's return type, which used the non-existent generic Object<T> instead of Preact's RefObject<T>. Closes #110
1 parent 7d4a2aa commit 6173e1a

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

‎build/bundle.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,29 @@ async function bundleDeclarations() {
206206
])
207207
await execa("shx", ["rm", "-rf", `${rootDir}/dist/src`])
208208
await execa("shx", ["rm", `${rootDir}/dist/index.js`])
209+
await fixDeclarationImports()
210+
}
211+
212+
/**
213+
* The framework declarations import the core types as `"../index"`, which
214+
* TypeScript rejects under `moduleResolution: node16/nodenext` (TS2835) since
215+
* this is an ESM package. `"../index.js"` resolves to `index.d.ts` under every
216+
* resolution mode (node10, node16/nodenext, bundler). See #110.
217+
*/
218+
async function fixDeclarationImports() {
219+
for (const framework of ["react", "preact", "solid", "vue", "angular", "qwik"]) {
220+
const file = `${rootDir}/dist/${framework}/index.d.ts`
221+
let contents: string
222+
try {
223+
contents = await readFile(file, "utf8")
224+
} catch {
225+
continue
226+
}
227+
await writeFile(
228+
file,
229+
contents.replace(/from (["'])\.\.\/index\1/g, "from $1../index.js$1")
230+
)
231+
}
209232
}
210233

211234
async function markoBuild() {

‎src/preact/index.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useEffect, useState, useRef } from "preact/hooks"
2+
import type { RefObject } from "preact"
23
import autoAnimate, {
34
AutoAnimateOptions,
45
AutoAnimationPlugin,
@@ -12,7 +13,7 @@ import autoAnimate, {
1213
*/
1314
export function useAutoAnimate<T extends Element>(
1415
options?: Partial<AutoAnimateOptions> | AutoAnimationPlugin
15-
): [Object<T>, (enabled: boolean) => void] {
16+
): [RefObject<T>, (enabled: boolean) => void] {
1617
const element = useRef<T>(null)
1718
const [controller, setController] = useState<
1819
AnimationController | undefined

0 commit comments

Comments
 (0)