-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Infer non returning function to be returning undefined
, not void
#36239
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
Comments
I like the current behavior. If I write a function with no |
Another case is if you are using a function of type |
I hate the following error. The compiler requires a return statement although I just intend to reject null. declare function f<T>(g: () => T): void;
f<undefined>(() => { }); // error |
Let's say today you write a function and there's no useful value to return. Then, next month, you decide that it'd be better if the function returned a number of some kind. Is this a breaking change in your API contract? If the non-returning version of the function was marked as If the non-returning version of the function was marked as const p = fn() || 2; would have been an error under What are we getting in return? The "confusion" around |
I disagree, for me, specified types/behavior trumps unspecified types/behavior. and I think that contradicts with your comments before that specified behavior should be annotated as so. Non-returning functions have a specified behavior too.
Anyway, if you plan not to accept this issue, is it possible to accept an new issue to allow this? function a(): undefined { }
function b(): unknown { } |
I think those are pretty reasonable, at least with |
@RyanCavanaugh #36239 (comment) is not the issue here. I mean the compiler can know that the return type can be undefined from the function body. This is an issue of type inferences rather than type checking. |
If the non-returning version of the function was marked as |
So I disagree with author's use case but the compiler should allow non-returning functions to return the |
I separated the issues into #36288 because declaring |
EDIT: it looks like #36288 might be a better place for me to raise the below case? Not really sure, as both issues seem relevant. I'm running into a situation where I have a At the call site, I'm trying to do: const res = await func();
if (res?.field) {
...
} however the compiler flags this an error, as optional chaining doesn't work on const res = await func();
if (res && res.field) {
...
} This seems weird to me, since I expect The only thing I can think to do, besides using the |
This would be very useful. Imagine a function a bit like found in immer where the return type is void, and an object is passed for the sole purpose of being mutated (it's actually a proxy that doesn't get mutated but that's how the api looks like from the outside) so you expect people to write something like: update(obj, draft => {
draft.name = 'the new name'
}) If one doesn't pay attention, it's very easy to return a transformed object and expect this change to be picked up. It won't do anything and there is no compilation error: update(obj, draft => newObjFromApi) // Oh no, this won't do anything. This is not a fiction by the way, this happened 5 minutes ago 😆 EDIT: Nevermind, it seems |
@sharno I agree I think the |
@RyanCavanaugh, perhaps TypeScript should complain about this code? typescript-eslint already does:
function stop(reason = 'unspecified') {}
function fn() {
return undefined
}
stop(fn()) It's possible to similarly misuse function fn() {}
new Promise<void>((resolve) => resolve(fn()))
// or
function bar(): void {
return fn()
} |
Search Terms
non returning, undefined instead of void, undefined or unknown instead of void
Suggestion
undefined
instead ofvoid
unknown
as a type of non returning functionsexamples:
https://www.typescriptlang.org/play/#code/MYewdgzgLgBAZjAvDAFASiQPhgbwL4CwAUMaJLHAIxKpoBcMArmACYCmcAlmGy1rjEIkiZaPABMNdA2YBrMCADuYfvmJA
Motivations
void
is an annoying type to deal with, has many inconsistencies and there's a ton of issues around it for its weird behavior like: #35850 , #35236 , #33420 ... etcNon-returning functions actually return
undefined
which is a more predictable type to deal with and narrow disjunctions with and even use optional chaining with.Use Cases
Many use cases appear while using promises where there's a need to catch an error but at the same time not deal with
void
Checklist
My suggestion meets these guidelines:
Not sure if this might break some code or not but I think any code that dealt with void should work while dealing with undefined?
The text was updated successfully, but these errors were encountered: