Skip to content

Commit a85163f

Browse files
committed
chore: revert ts type changes and use smaller way.
1 parent 3a45618 commit a85163f

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed

src/Immutable.tsx

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ export type CompareProps<T extends React.ComponentType<any>> = (
66
nextProps: Readonly<React.ComponentProps<T>>,
77
) => boolean;
88

9-
type ImmutableProps<T extends React.ComponentType<any>> = Omit<React.ComponentProps<T>, 'ref'>;
10-
119
/**
1210
* Create Immutable pair for `makeImmutable` and `responseImmutable`.
1311
*/
@@ -34,24 +32,24 @@ export default function createImmutable() {
3432
function makeImmutable<T extends React.ComponentType<any>>(
3533
Component: T,
3634
shouldTriggerRender?: CompareProps<T>,
37-
): React.ComponentType<React.ComponentProps<T>> {
35+
): T {
3836
const refAble = supportRef(Component);
3937

40-
const ImmutableComponent = (props: ImmutableProps<T>, ref: React.Ref<any>) => {
38+
const ImmutableComponent = function (props: any, ref: any) {
4139
const refProps = refAble ? { ref } : {};
4240
const renderTimesRef = React.useRef(0);
4341
const prevProps = React.useRef(props);
4442

4543
// If parent has the context, we do not wrap it
4644
const mark = useImmutableMark();
4745
if (mark !== null) {
48-
return <Component {...(props as any)} {...refProps} />;
46+
return <Component {...props} {...refProps} />;
4947
}
5048

5149
if (
52-
// Always trigger re-render if `shouldTriggerRender` is not provided
50+
// Always trigger re-render if not provide `notTriggerRender`
5351
!shouldTriggerRender ||
54-
shouldTriggerRender(prevProps.current as any, props as any)
52+
shouldTriggerRender(prevProps.current, props)
5553
) {
5654
renderTimesRef.current += 1;
5755
}
@@ -60,7 +58,7 @@ export default function createImmutable() {
6058

6159
return (
6260
<ImmutableContext.Provider value={renderTimesRef.current}>
63-
<Component {...(props as any)} {...refProps} />
61+
<Component {...props} {...refProps} />
6462
</ImmutableContext.Provider>
6563
);
6664
};
@@ -69,9 +67,7 @@ export default function createImmutable() {
6967
ImmutableComponent.displayName = `ImmutableRoot(${Component.displayName || Component.name})`;
7068
}
7169

72-
return refAble
73-
? (React.forwardRef(ImmutableComponent) as React.ComponentType<React.ComponentProps<T>>)
74-
: (ImmutableComponent as unknown as React.ComponentType<React.ComponentProps<T>>);
70+
return refAble ? React.forwardRef(ImmutableComponent) as unknown as T : (ImmutableComponent as T);
7571
}
7672

7773
/**
@@ -81,13 +77,14 @@ export default function createImmutable() {
8177
function responseImmutable<T extends React.ComponentType<any>>(
8278
Component: T,
8379
propsAreEqual?: CompareProps<T>,
84-
): React.ComponentType<React.ComponentProps<T>> {
80+
): T {
8581
const refAble = supportRef(Component);
8682

87-
const ImmutableComponent = (props: ImmutableProps<T>, ref: React.Ref<any>) => {
83+
const ImmutableComponent = function (props: any, ref: any) {
8884
const refProps = refAble ? { ref } : {};
8985
useImmutableMark();
90-
return <Component {...(props as any)} {...refProps} />;
86+
87+
return <Component {...props} {...refProps} />;
9188
};
9289

9390
if (process.env.NODE_ENV !== 'production') {
@@ -97,12 +94,8 @@ export default function createImmutable() {
9794
}
9895

9996
return refAble
100-
? (React.memo(React.forwardRef(ImmutableComponent), propsAreEqual) as React.ComponentType<
101-
React.ComponentProps<T>
102-
>)
103-
: (React.memo(ImmutableComponent, propsAreEqual) as unknown as React.ComponentType<
104-
React.ComponentProps<T>
105-
>);
97+
? React.memo(React.forwardRef(ImmutableComponent), propsAreEqual) as unknown as T
98+
: (React.memo(ImmutableComponent, propsAreEqual) as unknown as T);
10699
}
107100

108101
return {

0 commit comments

Comments
 (0)