Skip to content
This repository was archived by the owner on Nov 13, 2023. It is now read-only.

Commit 31d0d13

Browse files
committed
TypeScript: type children of imported components as React.ReactNode
1 parent a29fc09 commit 31d0d13

File tree

5 files changed

+28
-8
lines changed

5 files changed

+28
-8
lines changed

Changes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# master
2+
- TypeScript: type children of imported components as `React.ReactNode`.
23

34
# 4.2.0
45
- Add basic support for inherited type definitions in polymorphic variants e.g. `type color = [red | blue]`. No support for conversion at the moment.

examples/typescript-react-example/src/ImportHookDefault.gen.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,28 @@ import {default as defaultNotChecked} from './hookExample';
99
// In case of type error, check the type of 'make' in 'ImportHookDefault.re' and './hookExample'.
1010
export const makeTypeChecked: React.ComponentType<{
1111
readonly person: person;
12-
readonly children: JSX.Element;
12+
readonly children: React.ReactNode;
1313
readonly renderMe: ImportHooks_renderMe<string>
1414
}> = makeNotChecked;
1515

1616
// Export 'make' early to allow circular import from the '.bs.js' file.
1717
export const make: unknown = makeTypeChecked as React.ComponentType<{
1818
readonly person: person;
19-
readonly children: JSX.Element;
19+
readonly children: React.ReactNode;
2020
readonly renderMe: ImportHooks_renderMe<string>
2121
}>;
2222

2323
// In case of type error, check the type of 'default' in 'ImportHookDefault.re' and './hookExample'.
2424
export const defaultTypeChecked: React.ComponentType<{
2525
readonly person: person;
26-
readonly children: JSX.Element;
26+
readonly children: React.ReactNode;
2727
readonly renderMe: ImportHooks_renderMe<string>
2828
}> = defaultNotChecked;
2929

3030
// Export '$$default' early to allow circular import from the '.bs.js' file.
3131
export const $$default: unknown = defaultTypeChecked as React.ComponentType<{
3232
readonly person: person;
33-
readonly children: JSX.Element;
33+
readonly children: React.ReactNode;
3434
readonly renderMe: ImportHooks_renderMe<string>
3535
}>;
3636

examples/typescript-react-example/src/ImportHooks.gen.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ import {foo as fooNotChecked} from './hookExample';
99
// In case of type error, check the type of 'makeRenamed' in 'ImportHooks.re' and './hookExample'.
1010
export const makeRenamedTypeChecked: React.ComponentType<{
1111
readonly person: person;
12-
readonly children: JSX.Element;
12+
readonly children: React.ReactNode;
1313
readonly renderMe: renderMe<any>
1414
}> = makeRenamedNotChecked;
1515

1616
// Export 'makeRenamed' early to allow circular import from the '.bs.js' file.
1717
export const makeRenamed: unknown = makeRenamedTypeChecked as React.ComponentType<{
1818
readonly person: person;
19-
readonly children: JSX.Element;
19+
readonly children: React.ReactNode;
2020
readonly renderMe: renderMe<any>
2121
}>;
2222

examples/typescript-react-example/src/hookExample.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const foo = (x: {
66

77
type Props = {
88
readonly person: { readonly name: string; readonly age: number };
9-
readonly children: JSX.Element;
9+
readonly children: React.ReactNode;
1010
readonly renderMe: React.ComponentType<{
1111
randomString: string;
1212
readonly poly: string;

src/EmitJs.ml

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,30 @@ let rec emitCodeItem ~config ~emitters ~moduleItemsEmitter ~env ~fileName
174174
let type_ =
175175
match type_ with
176176
| Function
177-
({argTypes = [{aType = Object (_, fields)}]; retType} as function_)
177+
({
178+
argTypes = [({aType = Object (closedFlag, fields)} as argType)];
179+
retType;
180+
} as function_)
178181
when retType |> EmitType.isTypeFunctionComponent ~config ~fields ->
179182
let componentName =
180183
match importFile with "." | ".." -> None | _ -> Some importFile
181184
in
185+
let fields =
186+
fields
187+
|> List.map (fun (field : field) ->
188+
match
189+
field.nameJS = "children"
190+
&& field.type_ |> EmitType.isTypeReactElement ~config
191+
with
192+
| true -> {field with type_ = EmitType.typeReactChild ~config}
193+
| false -> field)
194+
in
195+
let function_ =
196+
{
197+
function_ with
198+
argTypes = [{argType with aType = Object (closedFlag, fields)}];
199+
}
200+
in
182201
Function {function_ with componentName}
183202
| _ -> type_
184203
in

0 commit comments

Comments
 (0)