diff --git a/src/Immutable.tsx b/src/Immutable.tsx index b8127d4..67a3b2d 100644 --- a/src/Immutable.tsx +++ b/src/Immutable.tsx @@ -6,8 +6,6 @@ export type CompareProps> = ( nextProps: Readonly>, ) => boolean; -type ImmutableProps> = Omit, 'ref'>; - /** * Create Immutable pair for `makeImmutable` and `responseImmutable`. */ @@ -34,10 +32,10 @@ export default function createImmutable() { function makeImmutable>( Component: T, shouldTriggerRender?: CompareProps, - ): React.ComponentType> { + ): T { const refAble = supportRef(Component); - const ImmutableComponent = (props: ImmutableProps, ref: React.Ref) => { + const ImmutableComponent: React.ForwardRefRenderFunction = (props, ref) => { const refProps = refAble ? { ref } : {}; const renderTimesRef = React.useRef(0); const prevProps = React.useRef(props); @@ -45,13 +43,13 @@ export default function createImmutable() { // If parent has the context, we do not wrap it const mark = useImmutableMark(); if (mark !== null) { - return ; + return ; } if ( // Always trigger re-render if `shouldTriggerRender` is not provided !shouldTriggerRender || - shouldTriggerRender(prevProps.current as any, props as any) + shouldTriggerRender(prevProps.current, props) ) { renderTimesRef.current += 1; } @@ -60,7 +58,7 @@ export default function createImmutable() { return ( - + ); }; @@ -70,8 +68,8 @@ export default function createImmutable() { } return refAble - ? (React.forwardRef(ImmutableComponent) as React.ComponentType>) - : (ImmutableComponent as unknown as React.ComponentType>); + ? (React.forwardRef(ImmutableComponent) as unknown as T) + : (ImmutableComponent as T); } /** @@ -81,13 +79,13 @@ export default function createImmutable() { function responseImmutable>( Component: T, propsAreEqual?: CompareProps, - ): React.ComponentType> { + ): T { const refAble = supportRef(Component); - const ImmutableComponent = (props: ImmutableProps, ref: React.Ref) => { + const ImmutableComponent: React.ForwardRefRenderFunction = (props, ref) => { const refProps = refAble ? { ref } : {}; useImmutableMark(); - return ; + return ; }; if (process.env.NODE_ENV !== 'production') { @@ -96,13 +94,10 @@ export default function createImmutable() { })`; } - return refAble - ? (React.memo(React.forwardRef(ImmutableComponent), propsAreEqual) as React.ComponentType< - React.ComponentProps - >) - : (React.memo(ImmutableComponent, propsAreEqual) as unknown as React.ComponentType< - React.ComponentProps - >); + return React.memo( + refAble ? React.forwardRef(ImmutableComponent) : ImmutableComponent, + propsAreEqual, + ) as unknown as T; } return {