What You'll Learn
You'll learn how to use interfaces to describe the shape of callable objects — essentially treating functions as structured contracts just like objects.
Why Interfaces for Functions?
You've seen interfaces describe objects with properties and methods. But in TypeScript (and JavaScript), functions are objects too. This means you can use an interface to define what a function's signature should look like — what parameters it accepts and what it returns.
This is especially useful when you need to pass around functions that must follow a strict contract, or when a function object has both a callable signature and additional properties.
Basic Function Interface Syntax
Use a call signature inside an interface:
interface Greeter {
(name: string): string;
}
const sayHello: Greeter = (name) => {
return `Hello, ${name}!`;
};
const result = sayHello("Alice"); // "Hello, Alice!"
The (name: string): string; syntax describes a function that takes a string parameter and returns a string.
When This Matters
This becomes powerful when you need callable objects with properties:
interface Counter {
(start: number): number;
interval: number;
}
const myCounter: Counter = ((start) => {
return start + myCounter.interval;
}) as Counter;
myCounter.interval = 5;
const result = myCounter(10); // 15
Here, Counter is both callable and has an interval property.
Key Takeaway: Interfaces can describe function signatures using call signature syntax, allowing you to enforce contracts on callable objects and even combine function behavior with properties.