In a webinar, someone asked about passing functions to TypeScript interfaces. I explained with some examples. In this tutorial, I will explain how to add functions to interfaces in TypeScript.
What is an Interface in TypeScript?
An interface in TypeScript is a way to define the shape of an object. It allows you to specify the types of properties and methods that an object should have, making your code more predictable and easier to debug.
Interfaces provide several benefits:
- Type Safety: Ensures that the objects conform to specific types.
- Code Readability: Makes the code easier to understand and maintain.
- Reusability: Interfaces can be reused across different parts of your application.
Read How to Return Multiple Values in TypeScript?
Add Functions to Interfaces in TypeScript
To add a function to an interface in TypeScript, you need to define the function signature within the interface. Let’s start with a basic example involving a Person
interface.
interface Person {
firstName: string;
lastName: string;
age: number;
getFullName(): string;
}
In this example, the Person
interface includes a getFullName
method, which returns a string. Here’s how you can implement this interface in a class.
class AmericanPerson implements Person {
firstName: string;
lastName: string;
age: number;
constructor(firstName: string, lastName: string, age: number) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
getFullName(): string {
return `${this.firstName} ${this.lastName}`;
}
}
const johnDoe = new AmericanPerson('John', 'Doe', 30);
console.log(johnDoe.getFullName()); // Output: John Doe
You can check out the output in the screenshot below after I executed the above TypeScript code.

Check out Generic Arrow Functions in TypeScript
Add Functions With Parameters to Interfaces in TypeScript
Let’s enhance our example by adding a method that takes parameters. Suppose we want to add a method to calculate the person’s birth year.
Here is the complete code to add functions with parameters to interfaces in TypeScript.
interface Person {
firstName: string;
lastName: string;
age: number;
getFullName(): string;
getBirthYear(currentYear: number): number;
}
class AmericanPerson implements Person {
firstName: string;
lastName: string;
age: number;
constructor(firstName: string, lastName: string, age: number) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
getFullName(): string {
return `${this.firstName} ${this.lastName}`;
}
getBirthYear(currentYear: number): number {
return currentYear - this.age;
}
}
const janeSmith = new AmericanPerson('Jane', 'Smith', 25);
console.log(janeSmith.getBirthYear(2024)); // Output: 1999
Here is the exact output in the screenshot below:

Read Pass a Function as a Parameter in TypeScript
Using Interfaces with Functions
Interfaces can also define the shape of standalone functions. For instance, you might have a Logger
interface for logging messages.
interface Logger {
logInfo(message: string): void;
logError(message: string): void;
}
class ConsoleLogger implements Logger {
logInfo(message: string): void {
console.log(`Info: ${message}`);
}
logError(message: string): void {
console.error(`Error: ${message}`);
}
}
const logger = new ConsoleLogger();
logger.logInfo('This is an informational message.'); // Output: Info: This is an informational message.
logger.logError('This is an error message.'); // Output: Error: This is an error message.
Conclusion
In this tutorial, I explained how to add functions to interfaces in TypeScript with examples. I have also shown how to add functions with parameters to interfaces in TypeScript.
You may also like:
- Default Function Parameters in TypeScript
- Get the Return Type of a Function in TypeScript
- How to Use Default Parameter Values in TypeScript?
I’m Bijay Kumar Sahoo, and I am honored to be awarded the Microsoft MVP. With over 18 years of experience in the IT industry, I got a chance to work on SharePoint Framework (SPFx) development, TypeScript, React, JavaScript, etc. My journey has taken me through esteemed organizations such as TCS, HP, and KPIT, where I have honed my skills and expanded my expertise. Check out more about me here.