diff --git a/packages/svelte/src/runtime/internal/lifecycle.js b/packages/svelte/src/runtime/internal/lifecycle.js index 27c8f67d11..e41d59059e 100644 --- a/packages/svelte/src/runtime/internal/lifecycle.js +++ b/packages/svelte/src/runtime/internal/lifecycle.js @@ -36,9 +36,7 @@ export function beforeUpdate(fn) { * * https://svelte.dev/docs#run-time-svelte-onmount * @template T - * @param {() => T extends Promise<() => any> - * ? "Returning a function asynchronously from onMount won't call that function on destroy" - * : T} fn + * @param {() => import('./private.js').NotFunction | Promise> | (() => any)} fn * @returns {void} */ export function onMount(fn) { diff --git a/packages/svelte/src/runtime/internal/private.d.ts b/packages/svelte/src/runtime/internal/private.d.ts index 50f4a9046b..ef2245256e 100644 --- a/packages/svelte/src/runtime/internal/private.d.ts +++ b/packages/svelte/src/runtime/internal/private.d.ts @@ -123,3 +123,8 @@ export interface Task { abort(): void; promise: Promise; } + +/** + * Anything except a function + */ +type NotFunction = T extends Function ? never : T; diff --git a/packages/svelte/test/types/on-mount.ts b/packages/svelte/test/types/on-mount.ts index 5f5dac71ee..a7271a28c1 100644 --- a/packages/svelte/test/types/on-mount.ts +++ b/packages/svelte/test/types/on-mount.ts @@ -51,8 +51,15 @@ onMount(async () => { }; }); -// @ts-expect-error async and return any +// async and return any onMount(async () => { const a: any = null as any; return a; }); + +// async and return function casted to any +// can't really catch this without also catching above +onMount(async () => { + const a: any = (() => {}) as any; + return a; +});