0

Consider the following code:

type Country = "usa" | "uk" | "canada";
function match({c1, c2}:{c1:Country, c2: Exclude<Country, c2>}){} //This is error
match('usa', 'usa'); //I want this to be a error
match('uk', 'usa'); //and this be ok

I want to force arguments c1 and c2 have different values when called.

1 Answer 1

2

You can use generics as follows:

function match<C extends Country>(c1: C, c2: Exclude<Country, C>){}

I will probably only work properly if the arguments are literal strings, though, so it might not be easy to use if you are planning to use something else (like variables) for the arguments.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.