Skip to content

Commit 15a627b

Browse files
committed
kerosene-ui: Make the UnwrapComponent utility type actually recursive
1 parent 5364828 commit 15a627b

File tree

1 file changed

+14
-27
lines changed

1 file changed

+14
-27
lines changed
Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,21 @@
11
/**
2-
* Used internally by `UnwrapComponent<T>` to unwrap a single layer decorator-wrapped component
3-
* @private
2+
* Unwraps the decorated typings from a decorator-wrapped components to provide the original type of the underlying
3+
* component. Useful in unit testing when stubbing decorators with the identity function.
44
*/
5-
export type _UnwrapComponent<T> = T extends React.MemoExoticComponent<
5+
type UnwrapComponent<T> = T extends React.MemoExoticComponent<
66
infer TMemoComponent
77
>
8-
? TMemoComponent
8+
? { 0: UnwrapComponent<TMemoComponent> }[TMemoComponent extends any
9+
? 0
10+
: never]
911
: T extends React.LazyExoticComponent<infer TLazyComponent>
10-
? TLazyComponent
12+
? { 0: UnwrapComponent<TLazyComponent> }[TLazyComponent extends any
13+
? 0
14+
: never]
1115
: "WrappedComponent" extends keyof T
12-
// @ts-ignore
13-
? T["WrappedComponent"]
16+
? {
17+
// @ts-ignore
18+
0: UnwrapComponent<T["WrappedComponent"]>;
19+
// @ts-ignore
20+
}[T["WrappedComponent"] extends any ? 0 : never]
1421
: T;
15-
16-
/**
17-
* Unwraps the decorated typings from a decorator-wrapped components to provide the original type of the underlying
18-
* component. Useful in unit testing when stubbing decorators with the identity function.
19-
*
20-
* Note: This will unwrap up to 8 layers of decorators as although TypeScript supports recursive types, it does not
21-
* allow recursive type arguments. If more levels are required, you may use the `_UnwrapComponent<T>` type to unwrap
22-
* a single layer at a time.
23-
*/
24-
export type UnwrapComponent<T> = _UnwrapComponent<
25-
_UnwrapComponent<
26-
_UnwrapComponent<
27-
_UnwrapComponent<
28-
_UnwrapComponent<
29-
_UnwrapComponent<_UnwrapComponent<_UnwrapComponent<T>>>
30-
>
31-
>
32-
>
33-
>
34-
>;

0 commit comments

Comments
 (0)