-
Notifications
You must be signed in to change notification settings - Fork 6
chore: revert ts type changes and use smaller way #51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -6,8 +6,6 @@ export type CompareProps<T extends React.ComponentType<any>> = ( | |||||||||||
| nextProps: Readonly<React.ComponentProps<T>>, | ||||||||||||
| ) => boolean; | ||||||||||||
|
|
||||||||||||
| type ImmutableProps<T extends React.ComponentType<any>> = Omit<React.ComponentProps<T>, 'ref'>; | ||||||||||||
|
|
||||||||||||
| /** | ||||||||||||
| * Create Immutable pair for `makeImmutable` and `responseImmutable`. | ||||||||||||
| */ | ||||||||||||
|
|
@@ -34,24 +32,24 @@ export default function createImmutable() { | |||||||||||
| function makeImmutable<T extends React.ComponentType<any>>( | ||||||||||||
| Component: T, | ||||||||||||
| shouldTriggerRender?: CompareProps<T>, | ||||||||||||
| ): React.ComponentType<React.ComponentProps<T>> { | ||||||||||||
| ): T { | ||||||||||||
| const refAble = supportRef(Component); | ||||||||||||
|
|
||||||||||||
| const ImmutableComponent = (props: ImmutableProps<T>, ref: React.Ref<any>) => { | ||||||||||||
| const ImmutableComponent: React.ForwardRefRenderFunction<any, any> = (props, ref) => { | ||||||||||||
| const refProps = refAble ? { ref } : {}; | ||||||||||||
| const renderTimesRef = React.useRef(0); | ||||||||||||
| const prevProps = React.useRef(props); | ||||||||||||
|
|
||||||||||||
| // If parent has the context, we do not wrap it | ||||||||||||
| const mark = useImmutableMark(); | ||||||||||||
| if (mark !== null) { | ||||||||||||
| return <Component {...(props as any)} {...refProps} />; | ||||||||||||
| return <Component {...props} {...refProps} />; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| 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 ( | ||||||||||||
| <ImmutableContext.Provider value={renderTimesRef.current}> | ||||||||||||
| <Component {...(props as any)} {...refProps} /> | ||||||||||||
| <Component {...props} {...refProps} /> | ||||||||||||
| </ImmutableContext.Provider> | ||||||||||||
| ); | ||||||||||||
| }; | ||||||||||||
|
|
@@ -70,8 +68,8 @@ export default function createImmutable() { | |||||||||||
| } | ||||||||||||
|
|
||||||||||||
| return refAble | ||||||||||||
| ? (React.forwardRef(ImmutableComponent) as React.ComponentType<React.ComponentProps<T>>) | ||||||||||||
| : (ImmutableComponent as unknown as React.ComponentType<React.ComponentProps<T>>); | ||||||||||||
| ? (React.forwardRef(ImmutableComponent) as unknown as T) | ||||||||||||
| : (ImmutableComponent as T); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /** | ||||||||||||
|
|
@@ -81,13 +79,13 @@ export default function createImmutable() { | |||||||||||
| function responseImmutable<T extends React.ComponentType<any>>( | ||||||||||||
| Component: T, | ||||||||||||
| propsAreEqual?: CompareProps<T>, | ||||||||||||
| ): React.ComponentType<React.ComponentProps<T>> { | ||||||||||||
| ): T { | ||||||||||||
| const refAble = supportRef(Component); | ||||||||||||
|
|
||||||||||||
| const ImmutableComponent = (props: ImmutableProps<T>, ref: React.Ref<any>) => { | ||||||||||||
| const ImmutableComponent: React.ForwardRefRenderFunction<any, any> = (props, ref) => { | ||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 与 建议使用更精确的类型来定义 同样,请注意这个改动可能会影响
Suggested change
|
||||||||||||
| const refProps = refAble ? { ref } : {}; | ||||||||||||
| useImmutableMark(); | ||||||||||||
| return <Component {...(props as any)} {...refProps} />; | ||||||||||||
| return <Component {...props} {...refProps} />; | ||||||||||||
| }; | ||||||||||||
|
|
||||||||||||
| 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<T> | ||||||||||||
| >) | ||||||||||||
| : (React.memo(ImmutableComponent, propsAreEqual) as unknown as React.ComponentType< | ||||||||||||
| React.ComponentProps<T> | ||||||||||||
| >); | ||||||||||||
| return React.memo( | ||||||||||||
| refAble ? React.forwardRef(ImmutableComponent) : ImmutableComponent, | ||||||||||||
| propsAreEqual, | ||||||||||||
| ) as unknown as T; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| return { | ||||||||||||
|
|
||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
将
ImmutableComponent的类型定义为React.ForwardRefRenderFunction<any, any>会导致props和ref失去类型信息,降低了类型安全性。虽然这可以避免在后续代码中使用as any,但更好的做法是提供更精确的类型。我建议使用
React.ElementRef<T>和React.ComponentPropsWithoutRef<T>来精确地定义ref和props的类型。请注意,这个改动可能会暴露出
shouldTriggerRender函数的类型不匹配问题。它的类型CompareProps<T>期望的参数是React.ComponentProps<T>,而ImmutableComponent的props类型是React.ComponentPropsWithoutRef<T>。根本原因可能是CompareProps<T>的定义,它或许应该使用React.ComponentPropsWithoutRef<T>。考虑到这是一个更深层次的问题,一个临时的解决方案可能是在调用shouldTriggerRender时进行类型断言,但这正是本次 PR 想要移除的。最好的方式是修复CompareProps<T>的类型定义。