Skip to content
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

Merged
merged 2 commits into from
Jun 19, 2023

Conversation

GrygrFlzr
Copy link
Member

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)

If you return any from onMount, it's an error.

This updated type definition solves both of these concerns:

  • VS Code now shows the following onMount type signature:
    function onMount<T>(
        fn: () => NotFunction<T> | Promise<NotFunction<T>> | (() => any)
    ): void
    This is a lot more readable for humans compared to the previous one, since it's clearer what type we're trying to exclude.
  • Any async function that returns any is no longer a type error.

This does also mean that explicitly typecasting a function into any will be allowed, but as per Conduitry's comments on the matter #8136 (comment):

If someone really has a good/weird reason for going that, they can add a ts-ignore.

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

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint

- uses a clearer type definition for editor tooltips
- will not catch functions typecasted into any
@vercel
Copy link

vercel bot commented Jun 8, 2023

@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
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

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>

@benmccann benmccann added this to the 4.x milestone Jun 9, 2023
@dummdidumm dummdidumm merged commit 54f72f4 into sveltejs:version-4 Jun 19, 2023
6 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants