Method Signatures in Interfaces
What you'll learn: How to define function types as methods inside interfaces with parameter and return type annotations.
The Core Idea
In interfaces, you can describe methods that objects should have. A method signature tells TypeScript what parameters the method accepts and what type it returns—without providing the actual implementation. Think of it like a contract that says "any object with this interface must have a method that looks like this."
You've already seen how to add function properties to interfaces. Method signatures are a slightly different syntax that feels more natural when describing object methods.
Two Ways to Define Methods
You can define methods in interfaces using either method shorthand or function property syntax:
interface Calculator {
// Method shorthand syntax
add(a: number, b: number): number;
// Function property syntax (you already know this)
subtract: (a: number, b: number) => number;
}
Both are valid, but method shorthand is more common and readable for typical object methods.
Writing Method Signatures
interface Greeter {
greet(name: string): string;
sayGoodbye(): void;
}
const myGreeter: Greeter = {
greet(name) {
return `Hello, ${name}!`;
},
sayGoodbye() {
console.log("Goodbye!");
}
};
const message = myGreeter.greet("Alice"); // string
Notice how the parameter name doesn't need a type annotation in the implementation—TypeScript infers it from the interface signature.
Key Takeaway: Method signatures in interfaces define the shape of functions using methodName(params): returnType syntax, creating clear contracts for what methods objects must implement.