Skip to content

onMount type allows returning function when async #9004

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

Closed
sidharthv96 opened this issue Jul 20, 2023 · 2 comments
Closed

onMount type allows returning function when async #9004

sidharthv96 opened this issue Jul 20, 2023 · 2 comments

Comments

@sidharthv96
Copy link

sidharthv96 commented Jul 20, 2023

Describe the bug

The documentation for providing a return function from onMount states

This behaviour will only work when the function passed to onMount synchronously returns a value. async functions always return a Promise, and as such cannot synchronously return a function.

But, the generated type in lifecycle.d.ts is

export declare function onMount(fn: () => any): void;

Which accepts return functions, even when async. This causes a silent failure, which is impossible to detect without reading the documentation. The error can happen when someone converts an existing onMount with a destroy callback to async.

Strictly typing it like below makes TS throw an error when a function is returned wrapped in a promise.

export declare function onMount(fn: () => void | Promise<void> | (() => void)): void;

Error after changing type.

image

Reproduction

<script lang="ts">
  import { onMount } from 'svelte';

  onMount(async () => {
  // ^ No error

    // .... await someThing

    return () => {
      // Destroyed will not be call as the function is async.
    };
  });
</script>

Logs

No response

System Info

"svelte": "^3.59.1"

Severity

annoyance

@Conduitry
Copy link
Member

This is already fixed in 4.0.0 via #8136. It was not included in v3 as it would be a breaking change.

@Conduitry Conduitry closed this as not planned Won't fix, can't repro, duplicate, stale Jul 20, 2023
@sidharthv96
Copy link
Author

Just saw that when checking the git blame of internals.
Thank you.

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

No branches or pull requests

2 participants