What is the call signature of an object literal type and how can they be used with generic types?

Functions can have properties, that’s what the object literal syntax is for: it allows to define a call signature and additional properties. Your two examples are equivalent because the second doesn’t define additional properties on the object literal. You can read more on that in the section on hybrid types.

Additionally, the object literal allows to define multiple call signatures for function overloads. You can create an object of such an interface with Object.assign:

interface Foo {
    (x: string): number,
    (x: number): string,
    bar: Array<any>,
}

const foo: Foo = Object.assign(function (x: any) {
    if (typeof x === 'string') {
        return parseInt(x);
    } else {
        return x.toString();
    }
}, {
    bar: []
});

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)