Skip to content

fix: update onMount type to allow async to return any #8714

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 7 additions & 3 deletions packages/svelte/src/runtime/internal/lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ export function beforeUpdate(fn) {
get_current_component().$$.before_update.push(fn);
}

/**
* Anything except a function
* @template T
* @typedef {T extends Function ? never : T} NotFunction
*/

/**
* The `onMount` function schedules a callback to run as soon as the component has been mounted to the DOM.
* It must be called during the component's initialisation (but doesn't need to live *inside* the component;
Expand All @@ -36,9 +42,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 {() => NotFunction<T> | Promise<NotFunction<T>> | (() => any)} fn
Copy link
Member Author

@GrygrFlzr GrygrFlzr Jun 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm actually surprised this works as expected.

Consider that NotFunction<T> currently only negates Function. In theory, this should not exclude Promise<T>. Indeed, if we change the onMount type definition to:

function onMount<T>(
    fn: () => NotFunction<T> | (() => any)
): void

fn can now be anything (including a Promise!), because the union of (not a function) and (a function) should logically be any. For some reason, a further union with Promise<NotFunction<T>> successfully narrows the type to exclude promises which return functions, and yet a standalone Promise<NotFunction<T>> type would block any non-async callback functions.

Is this behavior with TypeScript expected or a bug?
Even if it turns out to be a bug, we can instead narrow the conditional types:

type NotFunction<T> = T extends Function ? never : T;
type NotFunctionOrPromise<T> = T extends (Function | Promise<T>) ? never : T;
function onMount<T>(
    fn: () => NotFunctionOrPromise<T> | Promise<NotFunction<T>> | (() => any)
): void

Copy link
Member

@baseballyama baseballyama Jun 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is because the return type of the below code is Promise<Fuinction>.

onMount(async () => {
	return () => {}
});

It means below code doesn't have a type error.

type NotFunction<T> = T extends Function ? never : T;
type A<T> = () => NotFunction<T>

function onMountA<T>(param: A<T>) {
    return param()
}

onMountA(async () => {
	return () => {}
});

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was trying to wrap my head around this, and from this playground and hovering over the infered types it makes sense why this works - T is infered to be a certain type, and that is put into each of those, and if there's something left, it works. It works because through Promise<NotAFunction<T>> the T is narrowed differently to () => void instead of Promise<() => void>

* @returns {void}
*/
export function onMount(fn) {
Expand Down
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;
});