Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions packages/svelte/src/runtime/internal/lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> | Promise<import('./private.js').NotFunction<T>> | (() => any)} fn
* @returns {void}
*/
export function onMount(fn) {
Expand Down
5 changes: 5 additions & 0 deletions packages/svelte/src/runtime/internal/private.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,8 @@ export interface Task {
abort(): void;
promise: Promise<void>;
}

/**
* Anything except a function
*/
type NotFunction<T> = T extends Function ? never : T;
9 changes: 8 additions & 1 deletion packages/svelte/test/types/on-mount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});