You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Surprising Not all code paths return a value #18319
In our real-world scenario, it would not be sufficient to look for this syntactic pattern. Rather, the fact that the async function is assigned to a union containing void should be the trigger that silences this error.
Related bug in v3.7.2. noImplicitReturns doesn't affect the behavior.
// Error TS2366: Function lacks ending return statement and return// type does not include 'undefined'.functionf(x: number): number{if(typeofx==='number'){return1}}
Notably, this prevents you from expressing React props as unions:
import*asReactfrom'react'typeProps=|{a: number}|{b: string}// Error TS2366: Function lacks ending return statement and return// type does not include 'undefined'.functionMyComponent(props: Props): JSX.Element{if('a'inprops){return<div>ais{props.a}</div>}if('b'inprops){return<div>bis{props.b}</div>}}
For this example to type check, we need to add a catch-all return statement at the end of MyComponent's body, which is not actually needed since at that point, props has already been refined to never.
I'm not really sure if this error is "unexpected" or not. It's controlled by the "No implicit returns" flag and this is definitely an implicit return.
How is a void function not returning anything an implicit return? How come it works when using return-type annotation? This is definitely a bug. I don't want to hide all implicit return warnings, I just wish to hide those with an optional void as return type.
Activity
RyanCavanaugh commentedon Sep 7, 2017
Doesn't require async
I'm not really sure if this error is "unexpected" or not. It's controlled by the "No implicit returns" flag and this is definitely an implicit return.
chancancode commentedon Sep 13, 2017
Just wanted to point out that this generally works without errors with the "No implicit returns" flag:
Playground link
mhegazy commentedon Sep 19, 2017
PRs welcome
bcherny commentedon Dec 23, 2019
Related bug in v3.7.2.
noImplicitReturnsdoesn't affect the behavior.Playground Link
Notably, this prevents you from expressing React props as unions:
Playground Link
For this example to type check, we need to add a catch-all
returnstatement at the end ofMyComponent's body, which is not actually needed since at that point,propshas already been refined tonever.matthieusieben commentedon Jun 4, 2020
Not sure if related to this but I get the same behavior when returning
void:return voidshould be considered as a path that does not return a value, right?wycats commentedon Aug 18, 2020
Mohamed Hegazy (@mhegazy) since some time has gone by: is this still "PRs welcome"?
rohitkhurmi095 commentedon Mar 20, 2021
compilerOptions:{
"noImplicitReturns": false
}
Mahi commentedon Apr 8, 2022
How is a
voidfunction not returning anything an implicit return? How come it works when using return-type annotation? This is definitely a bug. I don't want to hide all implicit return warnings, I just wish to hide those with an optionalvoidas return type.