Skip to content

Commit

Permalink
Update types to match desired behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandrHoroshih committed Feb 9, 2024
1 parent 7d92322 commit 6905000
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions public-types/reflect.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ type Hooks = {
unmounted?: EventCallable<void> | (() => unknown);
};

/**
* `bind` object type:
* prop key -> store (unwrapped to reactive subscription) or any other value (used as is)
*
* Also handles some edge-cases like enforcing type inference for inlined callbacks
*/
type BindFromProps<Props> = {
[K in keyof Props]?: K extends UnbindableProps
? never
Expand All @@ -25,6 +31,19 @@ type BindFromProps<Props> = {
: Store<Props[K]> | Props[K];
};

/**
* Computes final props type based on Props of the view component and Bind object.
*
* Props that are "taken" by Bind object are made **optional** in the final type,
* so it is possible to owerrite them in the component usage anyway
*/
type FinalProps<Props, Bind extends BindFromProps<Props>> = Omit<
Props,
keyof Bind
> & {
[K in Extract<keyof Bind, keyof Props>]?: Props[K];
};

// relfect types
/**
* Operator that creates a component, which props are reactively bound to a store or statically - to any other value.
Expand All @@ -49,7 +68,7 @@ export function reflect<Props, Bind extends BindFromProps<Props>>(config: {
* This configuration is passed directly to `useUnit`'s hook second argument.
*/
useUnitConfig?: UseUnitConfig;
}): FC<Omit<Props, keyof Bind>>;
}): FC<FinalProps<Props, Bind>>;

// Note: FC is used as a return type, because tests on a real Next.js project showed,
// that if theoretically better option like (props: ...) => React.ReactNode is used,
Expand Down Expand Up @@ -83,7 +102,7 @@ export function createReflect<Props, Bind extends BindFromProps<Props>>(
*/
useUnitConfig?: UseUnitConfig;
},
) => FC<Omit<Props, keyof Bind>>;
) => FC<FinalProps<Props, Bind>>;

// list types
type PropsifyBind<Bind> = {
Expand Down Expand Up @@ -199,7 +218,7 @@ export function variant<
*/
useUnitConfig?: UseUnitConfig;
},
): FC<Omit<Props, keyof Bind>>;
): FC<FinalProps<Props, Bind>>;

// fromTag types
type GetProps<HtmlTag extends keyof ReactHTML> = Exclude<
Expand Down

0 comments on commit 6905000

Please sign in to comment.