Callables are interfaces with "bare" or unnamed method signatures:
type ValidNumber = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10;
interface Thing {
(): ValidNumber
toStringProp(): string
}
Constructing them is not entirely type-safe, so it's best to do a bit of extra work with helpers:
interface ThingCallable {
(): ValidNumber
}
interface ThingProps {
toStringProp(): string
}
type Thing = ThingCallable & ThingProps;
const thingCallable: ThingCallable = () => 7;
const thingMixin = { toStringProp() { return 'hi' } };
const thing: Thing = Object.assign(thingCallable, thingMixin);
Or, as suggested in the duplicate question using Object.assign
directly:
interface Thing {
(): ValidNumber
toStringProp(): string
}
const thing: Thing = Object.assign(
// Must be a valid () => ValidNumber
() => 9,
// Must be a valid superset of the other properties in Thing
{
toStringProp() { return 'hello'; }
}
);