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

Why is Promise<void> accepted as void #58556

Closed
1 task done
TarSzator opened this issue May 17, 2024 · 3 comments
Closed
1 task done

Why is Promise<void> accepted as void #58556

TarSzator opened this issue May 17, 2024 · 3 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@TarSzator
Copy link

Acknowledgement

  • I acknowledge that issues using this template may be closed without further explanation at the maintainer's discretion.

Comment

I am looking for an official explanation why TS accepts Promise as void.
Playground

When I provide a higher-order function that accepts a callback () => void I do not want user's to inject a function () => Promise<void> because I do not handle the rejection case of the Promise inside my higher-order function which might lead to an unhandled promise rejection.

Same goes from the other direction if by accident I put a callback () => Promise<void> into an higher-order function that only accepts () => void I would like to be informed that the higher-order function will not handle the Promise rejection correctly because it does not expect a Promise to be returned by the callback but void.

But I assume there was a thought process behind the decision to accept Promise as void but I could not find in the official reason.

@MartinJohns
Copy link
Contributor

This is working as intended. A callback returning void means "can be whatever, I don't care". If the user wants to return a promise, so be it. When they want to return an object, so be it.

https://github.com/microsoft/TypeScript/wiki/FAQ#why-are-functions-returning-non-void-assignable-to-function-returning-void

See also: #42709

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label May 17, 2024
@Jamesernator
Copy link

Same goes from the other direction if by accident I put a callback () => Promise<void> into an higher-order function that only accepts () => void I would like to be informed that the higher-order function will not handle the Promise rejection correctly because it does not expect a Promise to be returned by the callback but void.

Typescript-eslint has a rule for this if you really want to ensure you're not using this behaviour.

@TarSzator
Copy link
Author

Thank you @MartinJohns that helped

Thanks also @Jamesernator I knew about that but good that it is written here as well for others 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants