Is there a way to describe a relation between two parameters in TypeScript?

Suppose there's a function f that takes two parameters a: A and b: B, with A having a property prop of type B:

interface A {
  ...
  prop: B
  ...
}

interface B {
  ...
}

function f(a: A, b: B): R {
  ...
}

Is there a way in TypeScript to enforce through a type system, that the second parameter points to the same object of type B as the property prop in the first parameter?

i.e. a.prop === b

Upvotes: 1

Views: 372

Answers (1)

IMujagic
IMujagic

Reputation: 1259

I don't want to know the reason why you are sending it in that way but that what you want is technically not possible because at compile time you can only check types and not object values. Values you can check only in runtime - so that means you will need an "if" in your function that will check the equality of your A prop and B.

Upvotes: 2

Related Questions