Skip to content

Commit 54f72f4

Browse files
fix: update onMount type to allow async to return any (#8714)
--------- Co-authored-by: Simon H <[email protected]>
1 parent 0724261 commit 54f72f4

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

packages/svelte/src/runtime/internal/lifecycle.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ export function beforeUpdate(fn) {
3636
*
3737
* https://svelte.dev/docs#run-time-svelte-onmount
3838
* @template T
39-
* @param {() => T extends Promise<() => any>
40-
* ? "Returning a function asynchronously from onMount won't call that function on destroy"
41-
* : T} fn
39+
* @param {() => import('./private.js').NotFunction<T> | Promise<import('./private.js').NotFunction<T>> | (() => any)} fn
4240
* @returns {void}
4341
*/
4442
export function onMount(fn) {

packages/svelte/src/runtime/internal/private.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,8 @@ export interface Task {
123123
abort(): void;
124124
promise: Promise<void>;
125125
}
126+
127+
/**
128+
* Anything except a function
129+
*/
130+
type NotFunction<T> = T extends Function ? never : T;

packages/svelte/test/types/on-mount.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,15 @@ onMount(async () => {
5151
};
5252
});
5353

54-
// @ts-expect-error async and return any
54+
// async and return any
5555
onMount(async () => {
5656
const a: any = null as any;
5757
return a;
5858
});
59+
60+
// async and return function casted to any
61+
// can't really catch this without also catching above
62+
onMount(async () => {
63+
const a: any = (() => {}) as any;
64+
return a;
65+
});

0 commit comments

Comments
 (0)