Interface Typescript

mahabubr

Mahabubur Rahman

Posted on May 3, 2023

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
💖 💪 🙅 🚩
mahabubr
Mahabubur Rahman

Posted on May 3, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Interface Typescript
typescript Interface Typescript

May 3, 2023