I am trying to compile this Typescript fragment:
function foo(v: string) { return 'foo'; }
function bar(v: string | number) { return 'bar'; }
const notCallable: typeof foo | typeof bar = function() {} as any;
notCallable('a');
The compiler describes the type notCallableas ((v: string) => string) | ((v: string | number) => string)which looks normal but is not considered callable:
Cannot invoke an expression whose type does not have a call signature. Type '((v: string) => string) | ((v: string | number) => string) 'does not have compatible call signatures.
Note that if the parameter list matches, it works fine even if return types are different.
function foo(v: string) { return 'foo'; }
function bar(v: string) { return 0; }
const callable: typeof foo | typeof bar = function() {} as any;
callable('a');
This example is a simplified case that I initially discovered when trying to describe the concept of "continuous numeric functions of scale D3", which I tried to define as:
import { ScaleContinuousNumeric, ScaleTime } from 'd3-scale';
type ValidScale = ScaleContinuousNumeric<number, number> | ScaleTime<number, number>;
const s: ValidScale = function() {} as any;
s.domain([ 0, 1 ]);
, , ScaleContinuousNumeric, ScaleTime ?