Skip to content

Commit

Permalink
Fix for broken React functional component typings due to ElementType …
Browse files Browse the repository at this point in the history
…change in TypeScript 5.1+ (#101)
  • Loading branch information
volkanceylan authored Dec 20, 2023
1 parent 17b9cdf commit f553c37
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export function ShadowRoot(
}

export interface FunctionComponent<P = {}, T extends Element = JSX.Element> {
(props: P, context?: any): T | null
(props: PropsWithChildren<P>, context?: any): T | null
defaultProps?: Partial<P>
displayName?: string
}
Expand All @@ -235,7 +235,7 @@ export class Component<P = {}, T extends Element = JSX.Element> {

export { Component as PureComponent }

export type PropsWithChildren<P = unknown> = P & { children?: ReactNode | undefined }
export type PropsWithChildren<P> = P & { children?: ReactNode | undefined }

export type ComponentType<P = {}, T extends Element = JSX.Element> =
| ComponentClass<P, T>
Expand Down Expand Up @@ -489,7 +489,7 @@ interface EventHandlers<T> {
transitionend?: TransitionEventHandler<T> | undefined

// Custom events
[K: string]: ReactEventHandler<T> | undefined
[K: string]: EventHandler<any, T> | undefined
}

export interface DOMAttributes<T> {
Expand Down Expand Up @@ -2046,6 +2046,14 @@ interface ReactSVG {
view: SVGFactory
}

type JSXElementConstructor<P> =
| ((
props: P
) => ReactNode)
| (new(
props: P
) => Component<any, any>);

export namespace JSX {
// We don't just alias React.ElementType because React.ElementType
// historically does more than we need it to.
Expand All @@ -2058,8 +2066,7 @@ export namespace JSX {
// .propTypes assignability so we might as well drop it entirely here to
// reduce the work of the type-checker.
// TODO: Check impact of making React.ElementType<P = any> = React.JSXElementConstructor<P>
type ElementType = string

type ElementType = string | JSXElementConstructor<any>;
type Element = ReactElement

interface ElementAttributesProperty {
Expand Down

0 comments on commit f553c37

Please sign in to comment.