i want to define an overloaded function like
function first(n?: number) {
if (number === undefined) {
// returns a single Item
return items[0];
}
// returns an array of Item
return items.slice(0, n);
}
so that these statements type check:
const item: Item = first(); // no args, so return type is Item
const items: Array<Item> = first(5); // number arg, so return type is Array<Item>
flow knows that the first call to first
is going to result in n === undefined
(since it would complain if undefined
wasn't valid for n
) and it understands that it will then take the if branch, so i would think it could infer the return type is Item
, but everything i've tried either lets anything pass or always fails.
any idea if this is possible? thanks in advance internet.
any
cast... but it works. thanks. and i didn't know about that "tryflow" page either, that's super useful for fiddling. – Urban