I've been doing a lot of reading into variadic types and some of the new tuple stuff in TypeScript but am having trouble determining if what I'm trying to accomplish is simply not possible given the language capabilities currently.
The exact issue with the API currently can be seen here:
export type Themed<T> = { theme: T };
export type ThemedStyleAugmentationFn<P, T> = (
allProps: Partial<P> & Themed<T>
) => string;
export function augmentComponent<
P,
TA extends T,
C extends keyof JSX.IntrinsicElements | React.ComponentType<any>,
T extends object,
O extends object = {},
A extends keyof any = never
>(
styledComponent: StyledComponent<C, T, O, A>,
styleAugmentation: ThemedStyleAugmentationFn<P, TA>
): StyledComponent<C, T, O & Partial<P>, A> {
const augmented = styled(styledComponent)`
${styleAugmentation}
`;
return augmented as StyledComponent<C, T, O & Partial<P>, A>;
}
export function multiAugment<
P1 extends {},
TA1 extends T,
P2 extends {},
TA2 extends T,
C extends keyof JSX.IntrinsicElements | React.ComponentType<any>,
T extends object,
O extends object = {},
A extends keyof any = never
>(
styledComponent: StyledComponent<C, T, O, A>,
styleAugmentations: [ThemedStyleAugmentationFn<P1, TA1>, ThemedStyleAugmentationFn<P2, TA2>]
): StyledComponent<C, T, O & Partial<P1 & P2>, A> {
const augmented = styled(styledComponent)`
${styleAugmentations}
`;
return augmented as StyledComponent<C, T, O & Partial<P1 & P2>, A>;
}
In augmentComponent the important part of the signature is here: O & Partial<P>.
In multiAugment which explicitly takes a tuple of two ThemeStyleAugmentationFun it's here: O & Partial<P1 & P2>
I would like to be able to write a version of multiAugment that can take an arbitrary list of AugmentationFn and union them all together such that it would support any arity. Something that might return a version of O & Partial<P1 & P2 & P3 ... & P100> for example.
I'd like to avoid if possible needing to have a massive number of function overloads in order to support this as I could continue writing Tupled versions of increasing length.