S
S
Sergey Kotov2020-08-17 00:04:27
recursion
Sergey Kotov, 2020-08-17 00:04:27

How to write recursion correctly?

I am writing mixins, I can’t write the correct recursion

type Constructor = new (...args: any[]) => {};


export function mixin<TBase extends Constructor>(Base: TBase[], count = 0): TBase {
  const currentClass = Base[count];
  if (count === Base.length - 1){
    return class extends currentClass {};
  } else {
    count++;
    return class extends mixin(Base, count++) {};
  }
}
// должно получится типа так
// return class extends one
//   return class extends two
//     return class extends three

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aetae, 2020-08-17
@Aetae

Use ready-made libs (or look at their code).
Now mixins in ts are crutches on top of bugs .
Following your example: you can't just take an array and get reasonable types out of it. You need ovrloads for each individual type in order to get a union as an output.
I could stick something, but I won't because it hurts.)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question