Closed
Description
Bug Report
π Search Terms
- This expression is not callable.
- TS2349
- generic
- union type
π Version & Regression Information
Problem happens in all recent versions of typescript.
β― Playground Link
Playground link with relevant code
π» Code
interface EventEmitter<Mapping> {
on<EventName extends keyof Mapping>(eventName: EventName): void;
}
interface PayloadA {
readonly foo: number;
readonly bar: string;
}
interface PayloadB {
readonly foo: number;
readonly baz: boolean;
}
type EmitterA = EventEmitter<PayloadA>;
type EmitterB = EventEmitter<PayloadB>;
interface ModuleA {
readonly type: 'a';
readonly on: EmitterA['on'];
}
interface ModuleB {
readonly type: 'b';
readonly on: EmitterB['on'];
}
function doStuff(mod: ModuleA | ModuleB): void {
if (mod.type === 'a') {
mod.on('foo'); // works
} else {
mod.on('foo'); // works
}
mod.on('foo'); // doesnβt work
}
π Actual behavior
The following error is reported when calling mod.on('foo');
without narrowing its exact type:
This expression is not callable.
Each member of the union type '(<EventName extends keyof PayloadA>(eventName: EventName) => void) | (<EventName extends keyof PayloadB>(eventName: EventName) => void)' has signatures, but none of those signatures are compatible with each other.
π Expected behavior
Since mod.on('foo')
works in all possible branches when narrowing down the type I would expect it to also work without branching and narrowing.