Typescript: why does a union of a function only accept an intersection of the parameter?

Viewed 26

I will demonstrate my question with a simple example:

class Foo {
    foo() { }
}

class Bar {
    bar() { }
}

type FooHandler = (arg: Foo) => void;
type BarHandler = (arg: Bar) => void;

type AnyHandler = FooHandler | BarHandler;
declare var handler: AnyHandler;

handler(new Foo()); // Argument of type 'Foo' is not assignable to parameter of type 'Foo & Bar'.

Why does handler only accept the intersection Foo & Bar and not the union Foo | Bar? Is there any way to achieve this?

0 Answers
Related