Skip to content

Commit

Permalink
better util types
Browse files Browse the repository at this point in the history
  • Loading branch information
abailly-akamai committed Sep 25, 2024
1 parent 24b00f1 commit d30f145
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/manager/src/routes/utils.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ describe('strictLazyRouteComponent', () => {
Promise.resolve({ notAComponent: 'string' })
);

// @ts-expect-error - forcing the wrong type
const result = strictLazyRouteComponent(mockImporter, 'notAComponent');
await expect((result as any)()).rejects.toThrow(
'Export "notAComponent" is not a valid React component'
Expand Down
10 changes: 4 additions & 6 deletions packages/manager/src/routes/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { lazyRouteComponent } from '@tanstack/react-router';

import type { ComponentType } from 'react';

type AnyModule = { [key: string]: any };
type AnyModule = { [key: string]: React.ComponentType<unknown> };

/**
* This function is a wrapper around lazyRouteComponent that ensures the
Expand All @@ -12,7 +10,7 @@ type AnyModule = { [key: string]: any };
* By using a function overload we do just that.
*/
export function strictLazyRouteComponent<
T extends { default: ComponentType<any> }
T extends { default: React.ComponentType<unknown> }
>(importer: () => Promise<T>): ReturnType<typeof lazyRouteComponent>;
export function strictLazyRouteComponent<
T extends AnyModule,
Expand All @@ -27,11 +25,11 @@ export function strictLazyRouteComponent<T extends AnyModule>(
): ReturnType<typeof lazyRouteComponent> {
return lazyRouteComponent(() =>
importer().then((module) => {
let component: ComponentType<any>;
let component: React.ComponentType<unknown>;

if (exportName) {
if (exportName in module) {
component = module[exportName] as ComponentType<any>;
component = module[exportName] as React.ComponentType<unknown>;
} else {
throw new Error(`Export "${exportName}" not found in module`);
}
Expand Down

0 comments on commit d30f145

Please sign in to comment.