Reputation: 23
Is it possible to define a function interface in typescript?
for instance, when we have things such as
meth (a : {b: B, c: C}) : {b: B, c: C} {...}
we can write
interface A {
b : B;
c : C;
}
meth (a : A) : A {...}
however, when we have something like
meth (a : (b: B) => C) : (b: B) => C {...}
can we do something similar, like define a function type so that we can write
meth (a : A) : A {...}
again?
Upvotes: 2
Views: 132
Reputation: 250366
Interfaces support call signatures. Actually the syntax you use can be seen a shorthand for the more verbose full syntax:
interface A
{
(b: B) : C
}
function meth (a : A) : A { return a; }
The interface syntax also has the advantage of supporting multiple overloads for the function :
interface A {
(b: B): D
(d: D, b: B): C
}
function meth(a: A) : void
{
a(new B()); // Call overload with one argument
a(new D(), new B()); // Call overload with two arguments
}
meth((b: B | D, d?: D|B) => new D());
Upvotes: 1