Commit 41e57fde authored by Alejandro Celaya's avatar Alejandro Celaya Committed by Alejandro Celaya

Infer service names from component props when calling withServices HOC

parent 6713383b
...@@ -58,7 +58,7 @@ export const ServiceContext = createContext(fallbackInjector); ...@@ -58,7 +58,7 @@ export const ServiceContext = createContext(fallbackInjector);
*/ */
export function withServices< export function withServices<
Props extends Record<string, unknown>, Props extends Record<string, unknown>,
ServiceName extends string, ServiceName extends string & keyof Props,
>( >(
Component: ComponentType<Props>, Component: ComponentType<Props>,
serviceNames: ServiceName[], serviceNames: ServiceName[],
...@@ -71,17 +71,15 @@ export function withServices< ...@@ -71,17 +71,15 @@ export function withServices<
// Inject services, unless they have been overridden by props passed from // Inject services, unless they have been overridden by props passed from
// the parent component. // the parent component.
const services: Record<string, unknown> = {}; const services: Partial<Record<ServiceName, unknown>> = {};
for (const service of serviceNames) { for (const service of serviceNames) {
// Debugging check to make sure the store is used correctly. // Debugging check to make sure the store is used correctly.
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production' && service === 'store') {
if (service === 'store') {
/* istanbul ignore next - Ignore debug code */ /* istanbul ignore next - Ignore debug code */
throw new Error( throw new Error(
'Do not use `withServices` to inject the `store` service. Use the `useStore` hook instead', 'Do not use `withServices` to inject the `store` service. Use the `useStore` hook instead',
); );
} }
}
if (!(service in props)) { if (!(service in props)) {
services[service] = injector.get(service); services[service] = injector.get(service);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment