Validate a chain of mappers types

55 views Asked by At

I want to validate a chain/array of mappers at type level. The idea is that for each mapper the output should match the input of the next mapper in the list. And the first input and last output are validated by the ChainedMappers<From, To> generics.

So for example:

  • correctMappers is OK as it follows the chain: A -> B -> C -> D.
  • But incorrectMappers should FAIL because a mapper is missing: A -> B xxx C -> D.

I've tried with recursive types… but couldn't solve it. Is it doable?

interface Mapper<From, To> {
  map(from: From): To;
}

type A = /* ... */;
type B = /* ... */;
type C = /* ... */;
type D = /* ... */;

const mapperAB: Mapper<A, B> = /* ... */;
const mapperBC: Mapper<B, C> = /* ... */;
const mapperCD: Mapper<C, D> = /* ... */;

const correctMappers: ChainedMappers<A, D> = [
  mapperAB,
  mapperBC,
  mapperCD,
];

const incorrectMappers: ChainedMappers<A, D> = [
  mapperAB,
  // !!! Missing mapper from B to C !!!
  mapperCD,
];
0

There are 0 answers