Skip to content

Commit 84fa158

Browse files
committed
Improve React types
Signed-off-by: Winner95 <Winner95@users.noreply.github.com>
1 parent 3289e2b commit 84fa158

File tree

6 files changed

+39
-4
lines changed

6 files changed

+39
-4
lines changed

fixtures/Button.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import * as React from 'react';
22

33
/** Props for the Button component */
44
type ButtonProps = {

fixtures/ComponentWithoutType.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
22

3+
// @ts-ignore
34
const ComponentWithoutType = (props) => {
45
return <div>{props.title}</div>;
56
};

fixtures/ForwardedButton.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ type ButtonProps = {
66
label: string;
77
/** Optional click handler */
88
onClick?: () => void;
9+
children?: React.ReactNode;
910
};
1011

1112
// TypeError: Cannot read properties of undefined (reading 'length')
@@ -20,5 +21,5 @@ const Button: React.FC<ButtonProps> = ({ label, onClick }) => {
2021
// You can now get a ref directly to the DOM button:
2122
const ref = React.createRef();
2223

23-
return <ForwardedButton ref={ref}>{label}</ForwardedButton>;;
24+
return <ForwardedButton ref={ref} label={label}>{label}</ForwardedButton>;;
2425
};

fixtures/TooltipTarget.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
1-
import React from 'react';
1+
import React, { JSX } from 'react';
2+
3+
type TStringOrComponent = string | React.JSXElementConstructor<any>;
4+
5+
type PolymorphicProps<
6+
T extends TStringOrComponent,
7+
Props = {}
8+
> = Props &
9+
(T extends React.JSXElementConstructor<infer P>
10+
? P
11+
: T extends keyof JSX.IntrinsicElements
12+
? JSX.IntrinsicElements[T]
13+
: {}) & {
14+
as?: T;
15+
};
216

317
type TooltipTargetOwnProps = {
418
isDisabled?: boolean;
5-
children: React.ReactNode;
19+
children?: React.ReactNode;
620
};
721

822
type TooltipTargetProps<T extends TStringOrComponent> = PolymorphicProps<

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
},
3434
"homepage": "https://github.com/Winner95/typescript-react-function-component-props-handler#readme",
3535
"devDependencies": {
36+
"@types/react": "^19.2.6",
3637
"jest": "^30.2.0",
38+
"react": "^19.2.0",
3739
"react-docgen": "5.3.0"
3840
}
3941
}

yarn.lock

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,13 @@
683683
dependencies:
684684
undici-types "~7.16.0"
685685

686+
"@types/react@^19.2.6":
687+
version "19.2.6"
688+
resolved "https://registry.yarnpkg.com/@types/react/-/react-19.2.6.tgz#d27db1ff45012d53980f5589fda925278e1249ca"
689+
integrity sha512-p/jUvulfgU7oKtj6Xpk8cA2Y1xKTtICGpJYeJXz2YVO2UcvjQgeRMLDGfDeqeRW2Ta+0QNFwcc8X3GH8SxZz6w==
690+
dependencies:
691+
csstype "^3.2.2"
692+
686693
"@types/stack-utils@^2.0.3":
687694
version "2.0.3"
688695
resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-2.0.3.tgz#6209321eb2c1712a7e7466422b8cb1fc0d9dd5d8"
@@ -1071,6 +1078,11 @@ cross-spawn@^7.0.3, cross-spawn@^7.0.6:
10711078
shebang-command "^2.0.0"
10721079
which "^2.0.1"
10731080

1081+
csstype@^3.2.2:
1082+
version "3.2.3"
1083+
resolved "https://registry.yarnpkg.com/csstype/-/csstype-3.2.3.tgz#ec48c0f3e993e50648c86da559e2610995cf989a"
1084+
integrity sha512-z1HGKcYy2xA8AGQfwrn0PAy+PB7X/GSj3UVJW9qKyn43xWa+gl5nXmU4qqLMRzWVLFC8KusUX8T/0kCiOYpAIQ==
1085+
10741086
debug@^4.1.0, debug@^4.1.1, debug@^4.3.1:
10751087
version "4.4.3"
10761088
resolved "https://registry.yarnpkg.com/debug/-/debug-4.4.3.tgz#c6ae432d9bd9662582fce08709b038c58e9e3d6a"
@@ -2056,6 +2068,11 @@ react-is@^18.3.1:
20562068
resolved "https://registry.yarnpkg.com/react-is/-/react-is-18.3.1.tgz#e83557dc12eae63a99e003a46388b1dcbb44db7e"
20572069
integrity sha512-/LLMVyas0ljjAtoYiPqYiL8VWXzUUdThrmU5+n20DZv+a+ClRoevUzw5JxU+Ieh5/c87ytoTBV9G1FiKfNJdmg==
20582070

2071+
react@^19.2.0:
2072+
version "19.2.0"
2073+
resolved "https://registry.yarnpkg.com/react/-/react-19.2.0.tgz#d33dd1721698f4376ae57a54098cb47fc75d93a5"
2074+
integrity sha512-tmbWg6W31tQLeB5cdIBOicJDJRR2KzXsV7uSK9iNfLWQ5bIZfxuPEHp7M8wiHyHnn0DD1i7w3Zmin0FtkrwoCQ==
2075+
20592076
require-directory@^2.1.1:
20602077
version "2.1.1"
20612078
resolved "https://registry.yarnpkg.com/require-directory/-/require-directory-2.1.1.tgz#8c64ad5fd30dab1c976e2344ffe7f792a6a6df42"

0 commit comments

Comments
 (0)