2

I'm wondering if somethings as follows is possible in TypeScript, What I'm trying to achieve:

  1. If the type is inbox then obj should be of type interface IInbox.
  2. If the type is sent then obj should be of type interface ISent.

interface IInbox {

}

interface ISent {

}

class MailClient {
    delete(type: "inbox" | "sent", obj: IInbox | ISent) {

    }
}

let client = new MailClient();
client.delete('inbox', <ISent>{}); // should give compile error

2 Answers 2

3

You can define multiple signatures:

class MailClient {
    delete(type: "inbox", obj: IInbox);
    delete(type: "sent", obj: ISent)
    delete(type: "inbox" | "sent", obj: IInbox | ISent) {}
}

But that still won't have a compilation error because your interfaces are identical.
Because typescript is using duck typing then the empty object ({}) satisfies the type requirements.
If you differentiate between the two:

interface IInbox {
    a: string;
}

interface ISent {
    b: string;
}

Then you get your error:

client.delete('inbox', {} as ISent); // Argument of type '"inbox"' is not assignable to parameter of type '"sent"'

(code in playground)

Sign up to request clarification or add additional context in comments.

Comments

0

As far as I know this constraint is not possible, anyway you can easily write a very simple workaround as following:

class MailClient {
    delete(type: "inbox" | "sent", objI?: IInbox, objS?: ISent) {
        if ((type === "inbox" && typeof objS !== "undefined") ||
                 type === "send" && typeof objI !== "undefined") {
            throw new "Invalid parameter";    
        }
        let obj = (type === "inbox") ? objI : objS;
    }
}

This would do your job in a quite readable way.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.