Here is the code:
const func = (...args: [string, {a: true}] | [number, {b: true}]) => {
if (typeof args[0] === 'string') {
return args[1].a;
} else {
return args[1].b;
}
}
And typechecking failes to determine the shape of args variable. Is it somehow possible to do?
I expect that the example code doesn't show any TS errors
argsis not a discriminated union becausestringandnumberare not valid types for a discriminant (which must be a unit/singleton/literal type). There's no built-in method to narrow non-discriminated unions by checking properties. You could write your own type guard functions and use them, like this perhaps. Does that fully address your question? If so I could write up an answer explaining; if not, what am I missing? Please mention @jcalz if you reply to notify me.