Skip to content

Commit

Permalink
Update hoist-non-react-statics implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
aryaemami59 authored and markerikson committed Dec 10, 2024
1 parent a1fc886 commit 1cd1385
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions src/utils/hoistStatics.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
// Copied directly from:
// https://github.com/mridgway/hoist-non-react-statics/blob/main/src/index.js
// https://unpkg.com/browse/@types/[email protected].1/index.d.ts
// https://unpkg.com/browse/@types/[email protected].6/index.d.ts

/**
* Copyright 2015, Yahoo! Inc.
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/
import type {
ComponentType,
ForwardRefExoticComponent,
MemoExoticComponent,
} from 'react'
import type { ForwardRefExoticComponent, MemoExoticComponent } from 'react'
import { ForwardRef, Memo, isMemo } from '../utils/react-is'

const REACT_STATICS = {
Expand Down Expand Up @@ -70,19 +66,19 @@ function getStatics(component: any) {
}

export type NonReactStatics<
S extends ComponentType<any>,
Source,
C extends {
[key: string]: true
} = {},
> = {
[key in Exclude<
keyof S,
S extends MemoExoticComponent<any>
keyof Source,
Source extends MemoExoticComponent<any>
? keyof typeof MEMO_STATICS | keyof C
: S extends ForwardRefExoticComponent<any>
: Source extends ForwardRefExoticComponent<any>
? keyof typeof FORWARD_REF_STATICS | keyof C
: keyof typeof REACT_STATICS | keyof typeof KNOWN_STATICS | keyof C
>]: S[key]
>]: Source[key]
}

const defineProperty = Object.defineProperty
Expand All @@ -93,12 +89,15 @@ const getPrototypeOf = Object.getPrototypeOf
const objectPrototype = Object.prototype

export default function hoistNonReactStatics<
T extends ComponentType<any>,
S extends ComponentType<any>,
C extends {
Target,
Source,
CustomStatic extends {
[key: string]: true
} = {},
>(targetComponent: T, sourceComponent: S): T & NonReactStatics<S, C> {
>(
targetComponent: Target,
sourceComponent: Source,
): Target & NonReactStatics<Source, CustomStatic> {
if (typeof sourceComponent !== 'string') {
// don't hoist over string (html) components

Expand Down

0 comments on commit 1cd1385

Please sign in to comment.