I am using TypeScript Generics to specify the parameter types a function accepts. As seeing below, Foo and Bar, both have a function called isDirectory but seems to be invalid in the generic function. Can anyone explain me what I am missing here?
class Foo {
isDirectory(): boolean {
return true;
}
}
class Bar {
isDirectory(): boolean {
return true;
}
}
type XYZ = Foo | Bar;
function bas<XYZ>(x: XYZ) {
!!!!!!!! Property 'isDirectory' does not exist on type 'XYZ'.(2339)
return x.isDirectory();
}
I know that I can extend the generic type with extends, but why would that be needed in this case? Is there a solution without manually typing an interface?