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
A typeof check will not narrow a union like a discriminant property check, it will only narrow the type of the checked property. This has been the design since CFA was introduced I think, but I don't know if there are any plans to revisit this.
Is there a different way to either define the return type or do the refinement so that this will work without needing to refine each? Also tried leaving the entire object in tact but that doesn't work either for some reason:
declare function foo(): [number, string] | [string, number];
const res = foo();
if (typeof res[0] === 'number') {
console.log(res); // [number, string] | [string, number]
}
TypeScript Version: 3.3
Search Terms:
refine union
Code
Expected behavior:
Would expect
b
to be of typestring
hereActual behavior:
a
is refined tonumber
, whileb
remainsstring | number
Playground Link:
link
The text was updated successfully, but these errors were encountered: