Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit 9653ced

Browse files
committed
fix: fix dynamic hoc typing
1 parent a6ffaf0 commit 9653ced

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

framework/react/hoc.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@ import { useDeno, useRouter } from './hooks.ts'
1313

1414
type ReactNode = ReactChild | ReactFragment | ReactPortal
1515

16+
// Any prop that has a default prop becomes optional, but its type is unchanged
17+
// Undeclared default props are augmented into the resulting allowable attributes
18+
// If declared props have indexed properties, ignore default props entirely as keyof gets widened
19+
// Wrap in an outer-level conditional type to allow distribution over props that are unions
20+
type Defaultize<P, D> = P extends any
21+
? string extends keyof P ? P :
22+
& Pick<P, Exclude<keyof P, keyof D>>
23+
& Partial<Pick<P, Extract<keyof P, keyof D>>>
24+
& Partial<Pick<D, Exclude<keyof D, keyof P>>>
25+
: never
26+
27+
type ReactManagedProps<C, P> = C extends { defaultProps: infer D } ? Defaultize<P, D> : P
28+
1629
/**
1730
* `withRouter` allows you to use `useRouter` hook with class component.
1831
*
@@ -66,12 +79,12 @@ export function withDeno<T>(callback: () => (T | Promise<T>), revalidate?: numbe
6679
* }
6780
* ```
6881
*
69-
* @param {Function} factory - load factory.
82+
* @param {Function} factory - dynamic loading factory.
7083
*/
7184
export function dynamic<T extends ComponentType<any>>(
7285
factory: () => Promise<{ default: T }>
73-
): ComponentType<ComponentPropsWithRef<T> & { fallback?: ReactNode }> {
74-
const DynamicComponent = ({ fallback, ...props }: ComponentPropsWithRef<T> & { fallback?: ReactNode }) => {
86+
): ComponentType<ReactManagedProps<T, ComponentPropsWithRef<T>> & { fallback?: ReactNode }> {
87+
const DynamicComponent = ({ fallback, ...props }: ReactManagedProps<T, ComponentPropsWithRef<T>> & { fallback?: ReactNode }) => {
7588
const [mod, setMod] = useState<{ default: T } | null>(null)
7689

7790
useEffect(() => {

0 commit comments

Comments
 (0)