DEV Community

Mahabubur Rahman
Mahabubur Rahman

Posted on

Interface Typescript

Using Object

interface IUser {
    name: string;
    age: number;
}
Enter fullscreen mode Exit fullscreen mode
const userWithInterface: IUser = {
    name: "interface",
    age: 200,
};
Enter fullscreen mode Exit fullscreen mode

Extend Interface

interface IExtendUser extends IUser {
    role: string;
}
Enter fullscreen mode Exit fullscreen mode
const user1: IExtendUser = {
    age: 200,
    name: "Extend Interface",
    role: "Admin",
};
Enter fullscreen mode Exit fullscreen mode

Using Function

interface IAddNumbers {
    (num1: number, num2: number): number;
}
Enter fullscreen mode Exit fullscreen mode
const addNumbers: IAddNumbers = (num1, num2) => num1 + num2;
Enter fullscreen mode Exit fullscreen mode

Using Array

interface IRollNumbers {
    [index: number]: number;
}
Enter fullscreen mode Exit fullscreen mode
const rollNumbers: IRollNumbers = [1, 4, 5];
Enter fullscreen mode Exit fullscreen mode

Top comments (0)