Is it possible to refine tuples based on type checks similar to how null checks can be refined using === null
? I am trying to figure this out but haven't had any success so far. Here is a link to the TypeScript playground.
// refining against null works
declare function fooA(): [string, null] | [null, string];
function A() {
const x = fooA();
if (x[0] === null) {
const y = x[1]; // y = string
}
}
// attempting to refine based on type = string, fails
declare function isString(x: any): x is string;
declare function fooB(): [string, number] | [number, string];
function B() {
const x = fooB();
if (isString(x[0])) {
const y = x[1]; // y = string | number
}
}