2

The following example should not be considered as a real use case. It should only help to understand the question.

function myFunc<T1 extends string, T2 extends string>(s1: T1, s2: T2): string {
  return "";
}

Is it possible to ensure that T1 !== T2, hence that it is not possible to call myFunc like myFunc("MyValue", "MyValue")?

1 Answer 1

4

I think I figured it out.

function myFunc<T1 extends string, T2 extends string>(s1: T1, s2: T2 extends T1 ? never : T2): string {
  return "";
}
Sign up to request clarification or add additional context in comments.

2 Comments

Color me surprised, that totally works. Was about to comment that it's not possible until I tried it myself just now. Learned something new today.
@Andrew How does this color you: github.com/sno2/bf

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.