-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
fix: update onMount type to allow async to return any #8714
Conversation
- uses a clearer type definition for editor tooltips - will not catch functions typecasted into any
@GrygrFlzr is attempting to deploy a commit to the Svelte Team on Vercel. A member of the Team first needs to authorize it. |
* @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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 () => {}
});
There was a problem hiding this comment.
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>
Building off #8136, the current type
onMount
definition is a bit unfriendly in VS Code tooltip. There was also an additional concern by dummdidumm prior to it being merged: #8136 (comment)This updated type definition solves both of these concerns:
This does also mean that explicitly typecasting a
function
intoany
will be allowed, but as per Conduitry's comments on the matter #8136 (comment):It's not a big problem to allow explicit opt-outs anyway.
I have opted to not add a changelog entry, because the current breaking change entry pointing to #8136 is very much still adequate to cover the changes from this PR. This PR technically even loosens the type definition to allow
any
-returning async functions inside onMount.Before submitting the PR, please make sure you do the following
feat:
,fix:
,chore:
, ordocs:
.Tests
pnpm test
and lint the project withpnpm lint