Bill Software Engineer
Bill Software Engineer

Reputation: 7792

in typescript, how do I implement a interface with a callback?

I created an interface in typescript with callback, how do I implement it?

interface LoginCallback{
    Error: boolean,
    UserInfo: {
        Id: string,
        OrganizationId: string
    }
}

interface IntegrationInterface {
    Init(): void;
    LogIn(UserName: string, Password: string, LoginCallback:LoginCallback): void;
}

Upvotes: 0

Views: 159

Answers (1)

tony
tony

Reputation: 1326

The way you declared LoginCallback means it's just an object rather than a function. I assume this is what you wanted:

interface LoginCallback {
    (Error: boolean, UserInfo: { Id: string, OrganizationId: string }): void;
}

interface IntegrationInterface {
    Init(): void;
    LogIn(UserName: string, Password: string, LoginCallback: LoginCallback): void;
}

Then to implement the interface you can do:

class IntegrationImpl implements IntegrationInterface {
    Init() {
       //... 
    }
    LogIn(UserName: string, Password: string, LoginCallback: LoginCallback) {
     //...   
    }

}

Upvotes: 1

Related Questions