DEV Community

Discussion on: Introducing The Recursive `Pipe` and `Compose` Types

Collapse
 
lexlohr profile image
Alex Lohr

Great article and wow, what a complex and powerful type! Is there a way to get the actual argument (and not its type) recursively? I'm currently trying to create such a type for Solid.js' setStore function, which has basically the following interface:

setStore<Store>(
  ...selectors: StoreSelectors<Store¹>[],
  setter: StoreSetter<Store²>
) => void
Enter fullscreen mode Exit fullscreen mode

A selector can be a keyof Item, (keyof Item)[], a range { from: number, to: number } or a filter function. A setter can either be a DeepPartial value of the selected type, undefined or a function that receives the current Item and returns aforesaid values.

¹: each subsequent selector should not receive the Store, but the selection within it.

²: the setter should receive the selection of the last selector.

However, a selector could also be a string or number if the selected item inside the store was an object or array. Unfortunately, if I use

export type SetStoreFunction<Store> = <Args extends any[]>(
  ...args: Args & SetStoreArgs<Store, Args>
) => void;

setStore<{ test: 1, test2: { test3: 2 }}>('test2', 'test3', 3);
Enter fullscreen mode Exit fullscreen mode

I get the error that my Selectors are matching undefined. Any ideas or pointers on what I'm doing wrong?