This project is read-only.

Interface with multiple call signatures

Topics: General
Feb 17, 2014 at 2:11 PM
I'm not sure what best to call this, so I'll explain by example. I'm trying to write an interface that'll represent something like the Func delegate in .NET.

For example, this works:
export interface Func0<TResult>
  (): TResult;

export interface Func1<TResult, T0>
  (arg: T0): TResult;

export interface Func2<TResult, T0, T1>
But can I combine all of these signatures into a single interface (Func), and have the compiler infer which I'm referring to based on the number of types specified?

By the way, I know I can use something like:
var func: () => number;
But this is mainly just curiosity.
Feb 17, 2014 at 3:42 PM
I don't believe this is possible at the moment.

Marked as answer by Barguast on 2/17/2014 at 11:24 AM
Feb 17, 2014 at 7:24 PM
Edited Feb 17, 2014 at 7:26 PM
Looks like it. Thanks.

I've added a vote to the workitem. A (somewhat ugly) workaround is:
    export interface Action0 {
        (): void;

    export interface Action1<T1> {
        (arg: T1): void;

    export interface Action2<T1, T2> {
        (arg1: T1, arg2: T2): void;

    export interface Action3<T1, T2, T3> {
        (arg1: T1, arg2: T2, arg3: T3): void;
I have similar for Func and PromiseFunc, e.g.:
    export interface Func2<T1, T2, TResult> {
        (arg1: T1, arg2: T2): TResult;

    export interface PromiseFunc2<T1, T2, TResult> {
        (arg1: T1, arg2: T2): JQueryPromise<TResult>;
Using these helps to make some of the more complex function interfaces looks a bit more readable. Hopefully the 'overloaded' interfaces (if that's the right term) will be supported at some point.