0

I have one array of functions, data will pass in each function from first to second, from second to third ... to last, it will work like pipe(), I create type that will verify that output of one function will coincide with input of next function

type Hooks<T1, T2, T3, T4, T5, T6, T7, T8, T9> =
  [ (e: T1) => T2 ]
  | [ (e: T1) => T2, (e: T2) => T3 ]
  | [ (e: T1) => T2, (e: T2) => T3, (e: T3) => T4 ]
  | [ (e: T1) => T2, (e: T2) => T3, (e: T3) => T4, (e: T4) => T5 ]
  | [ (e: T1) => T2, (e: T2) => T3, (e: T3) => T4, (e: T4) => T5, (e: T5) => T6 ]
  | [ (e: T1) => T2, (e: T2) => T3, (e: T3) => T4, (e: T4) => T5, (e: T5) => T6, (e: T6) => T7 ]
  | [ (e: T1) => T2, (e: T2) => T3, (e: T3) => T4, (e: T4) => T5, (e: T5) => T6, (e: T6) => T7, (e: T7) => T8 ]
  | [ (e: T1) => T2, (e: T2) => T3, (e: T3) => T4, (e: T4) => T5, (e: T5) => T6, (e: T6) => T7, (e: T7) => T8, (e: T8) => T9 ];

but problem here is that is required to pass all types in generic

const example: Hooks<number, string, boolean, never, never, never, never, never, never> = [
  e => e.toString(),
  (e: string) => Boolean(e),
];

Exist method to infer type from function argument ?

Something like

type Hooks =
  [ (e: infer T1) => infer T2 ]
  | [ (e: T1) => T2, (e: T2) => infer T3 ]
  | [ (e: T1) => T2, (e: T2) => T3, (e: T3) => T4 ];   // of course this syntax not valid

or can be just method to send less generic arguments but with saving number of functions in array and ensure that each output will coincide with input of next function

Ken White
  • 123,280
  • 14
  • 225
  • 444
HardCoreQual
  • 331
  • 1
  • 3
  • 11
  • 1
    Does this answer your question? [Generic TypeScript type for an array of successive mapping functions](https://stackoverflow.com/questions/72762840/generic-typescript-type-for-an-array-of-successive-mapping-functions) – Tobias S. Aug 07 '22 at 00:29
  • No, I search a type that can do it, don't a function – HardCoreQual Aug 07 '22 at 00:38
  • 1
    The problem is that types are not powerful enough to do this. The only option is using a generic function, which can `infer` types based on the arguments. – Tobias S. Aug 07 '22 at 00:40
  • You can't infer generic type parameters from a value this way; if that's the question then it's a duplicate of [this one](https://stackoverflow.com/q/69976058/2887218). You have to use a helper function if you want to get the compiler to infer things like that; if you're okay with that I can write up an answer but it does seem to be a duplicate of the one TobiasS showed. Which one is your question? – jcalz Aug 07 '22 at 00:47
  • seems like it require declare type in each fn const result = mappingSequence(23, [ (a) => a * 60, (b) => 3, (c) => ({ duration: c }), ]); // here duration is any – HardCoreQual Aug 07 '22 at 00:47
  • If you will accept a helper function: The only way to get the inference you want is to have one type parameter per function input. Given that your `Hooks` type is already a large-ish finite collection of function types, you can get that inference with a similar large-ish finite collection of call signatures in the helper function, like [this](https://tsplay.dev/WPR6ZN). If that works for you I can write that up as an answer. Let me know either way. – jcalz Aug 07 '22 at 01:16
  • think yes, seems like is impossible to write a function that will receive an object that can have ~20 different Hooks list – HardCoreQual Aug 07 '22 at 07:23
  • So, do you want me to write up an answer or not? Please "@jcalz" mention me if you want me to be notified. – jcalz Aug 07 '22 at 18:50

0 Answers0