Currently, I'm having trouble writing a function that is able to handle both arrays and objects without running into conflicts. How can I instruct Typescript to avoid searching for length
if the input is an object, or for property foo
if it's an array?
interface Stuff {
list: [] | Obj;
}
interface Obj {
foo: "bar"
}
function generateSomething(data:Stuff) {
console.log(data.list.length) // Property 'length' does not exist on type 'Obj'
console.log(data.list.foo) // Property 'foo' does not exist on type '[]'
}