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
exportconstisBoolean=(value: unknown): value is boolean=>typeofvalue==='boolean';constuseAdapter=(syncDependencies: string[],asyncDependencies: string[]|boolean)=>(dependencies: string[]): [boolean,boolean]=>{constuseAsync=isBoolean(asyncDependencies)
? asyncDependencies
: asyncDependencies.reduce((prev,dep)=>prev||dependencies.includes(dep),false)constuse=useAsync||syncDependencies.reduce((prev,dep)=>prev||dependencies.includes(dep),false)return[use,useAsync]}
🙁 Actual behavior
TypeScript thinks that syncDependencies.reduce() returns a string. If i remove useAsync || in front of the reduce statement or place it after the reduce statement || useAsync it is detected as a boolean
🙂 Expected behavior
The reduce statement should return a value of type boolean because the value actually is a boolean
This is a known design limitation in our type inference algorithm. See here. Basically the issue in the failing repros is that the false argument is contextually typed by boolean, which causes us to preserve the literal type false. We then use that type as the inferred type for prev, which locks the inference, at which point we can't infer the wider type boolean returned by the arrow function. It's unfortunate, but at this point we don't have a better solution. The workaround is to write false as boolean instead just false.
Activity
ahejlsberg commentedon Nov 8, 2021
This is a known design limitation in our type inference algorithm. See here. Basically the issue in the failing repros is that the
falseargument is contextually typed byboolean, which causes us to preserve the literal typefalse. We then use that type as the inferred type forprev, which locks the inference, at which point we can't infer the wider typebooleanreturned by the arrow function. It's unfortunate, but at this point we don't have a better solution. The workaround is to writefalse as booleaninstead justfalse.andrewbranch commentedon Nov 8, 2021
Duplicate of #46438, see also #36554
ivanhofer commentedon Nov 9, 2021
Thanks Anders Hejlsberg (@ahejlsberg) for the good explanation and the workaround!
jcalz commentedon Oct 9, 2022
Fixed by #48380, it seems