1- import { supportRef } from 'rc-util/lib/ref' ;
1+ import { supportRef } from '@ rc-component/ util/lib/ref' ;
22import * as React from 'react' ;
33
44export type CompareProps < T extends React . ComponentType < any > > = (
55 prevProps : Readonly < React . ComponentProps < T > > ,
66 nextProps : Readonly < React . ComponentProps < T > > ,
77) => boolean ;
88
9+ type ImmutableProps < T extends React . ComponentType < any > > = Omit < React . ComponentProps < T > , 'ref' > ;
10+
911/**
1012 * Create Immutable pair for `makeImmutable` and `responseImmutable`.
1113 */
@@ -32,24 +34,24 @@ export default function createImmutable() {
3234 function makeImmutable < T extends React . ComponentType < any > > (
3335 Component : T ,
3436 shouldTriggerRender ?: CompareProps < T > ,
35- ) : T {
37+ ) : React . ComponentType < React . ComponentProps < T > > {
3638 const refAble = supportRef ( Component ) ;
3739
38- const ImmutableComponent = function ( props : any , ref : any ) {
40+ const ImmutableComponent = ( props : ImmutableProps < T > , ref : React . Ref < any > ) => {
3941 const refProps = refAble ? { ref } : { } ;
4042 const renderTimesRef = React . useRef ( 0 ) ;
4143 const prevProps = React . useRef ( props ) ;
4244
4345 // If parent has the context, we do not wrap it
4446 const mark = useImmutableMark ( ) ;
4547 if ( mark !== null ) {
46- return < Component { ...props } { ...refProps } /> ;
48+ return < Component { ...( props as any ) } { ...refProps } /> ;
4749 }
4850
4951 if (
50- // Always trigger re-render if not provide `notTriggerRender`
52+ // Always trigger re-render if `shouldTriggerRender` is not provided
5153 ! shouldTriggerRender ||
52- shouldTriggerRender ( prevProps . current , props )
54+ shouldTriggerRender ( prevProps . current as any , props as any )
5355 ) {
5456 renderTimesRef . current += 1 ;
5557 }
@@ -58,7 +60,7 @@ export default function createImmutable() {
5860
5961 return (
6062 < ImmutableContext . Provider value = { renderTimesRef . current } >
61- < Component { ...props } { ...refProps } />
63+ < Component { ...( props as any ) } { ...refProps } />
6264 </ ImmutableContext . Provider >
6365 ) ;
6466 } ;
@@ -67,7 +69,9 @@ export default function createImmutable() {
6769 ImmutableComponent . displayName = `ImmutableRoot(${ Component . displayName || Component . name } )` ;
6870 }
6971
70- return refAble ? React . forwardRef ( ImmutableComponent ) : ( ImmutableComponent as any ) ;
72+ return refAble
73+ ? ( React . forwardRef ( ImmutableComponent ) as React . ComponentType < React . ComponentProps < T > > )
74+ : ( ImmutableComponent as unknown as React . ComponentType < React . ComponentProps < T > > ) ;
7175 }
7276
7377 /**
@@ -77,14 +81,13 @@ export default function createImmutable() {
7781 function responseImmutable < T extends React . ComponentType < any > > (
7882 Component : T ,
7983 propsAreEqual ?: CompareProps < T > ,
80- ) : T {
84+ ) : React . ComponentType < React . ComponentProps < T > > {
8185 const refAble = supportRef ( Component ) ;
8286
83- const ImmutableComponent = function ( props : any , ref : any ) {
87+ const ImmutableComponent = ( props : ImmutableProps < T > , ref : React . Ref < any > ) => {
8488 const refProps = refAble ? { ref } : { } ;
8589 useImmutableMark ( ) ;
86-
87- return < Component { ...props } { ...refProps } /> ;
90+ return < Component { ...( props as any ) } { ...refProps } /> ;
8891 } ;
8992
9093 if ( process . env . NODE_ENV !== 'production' ) {
@@ -94,8 +97,12 @@ export default function createImmutable() {
9497 }
9598
9699 return refAble
97- ? React . memo ( React . forwardRef ( ImmutableComponent ) , propsAreEqual )
98- : ( React . memo ( ImmutableComponent , propsAreEqual ) as any ) ;
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+ > ) ;
99106 }
100107
101108 return {
0 commit comments