Skip to content

Commit

Permalink
Add intersection types in react-native-codegen for TypeScript (#35247)
Browse files Browse the repository at this point in the history
Summary:
Refer to "Support intersection of object types, `{...a, ...b, ...}` in Flow is equivalent to `a & b & {...}` in TypeScript, the order and the form is not important." in #34872

In this change I also extract the common part from `getTypeAnnotation` and `getTypeAnnotationForArray` into `getCommonTypeAnnotation`. Most of the differences are about `default` in the schema. After a schema is generated from `getCommonTypeAnnotation` successfully, `getTypeAnnotation` will fill `default` if necessary, while `getTypeAnnotationForArray` does nothing.

## Changelog

[General] [Changed] - Add intersection types in react-native-codegen for TypeScript

Pull Request resolved: #35247

Test Plan: `yarn jest react-native-codegen` passed

Reviewed By: cipolleschi

Differential Revision: D41105744

Pulled By: lunaleaps

fbshipit-source-id: cd250a9594a54596a20ae26e57a1c801e2047703
  • Loading branch information
ZihanChen-MSFT authored and facebook-github-bot committed Nov 10, 2022
1 parent 63a4539 commit 813fd04
Show file tree
Hide file tree
Showing 5 changed files with 261 additions and 158 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const tsExtraCases = [
'ARRAY2_PROP_TYPES_NO_EVENTS',
'PROPS_AND_EVENTS_WITH_INTERFACES',
];
const ignoredCases = ['ARRAY_PROP_TYPES_NO_EVENTS'];
const ignoredCases = [];

compareSnaps(
flowFixtures,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,13 @@ export interface ModuleProps extends ViewProps {
array_of_array_of_object_required_in_file: ReadonlyArray<
ReadonlyArray<ObjectType>
>;
// Nested array of array of object types (with spread)
array_of_array_of_object_required_with_spread: ReadonlyArray<
ReadonlyArray<
Readonly<ObjectType & {}>,
>,
>,
}
export default codegenNativeComponent<ModuleProps>(
Expand Down Expand Up @@ -584,6 +591,9 @@ export interface ModuleProps extends ViewProps {
// Nested array of array of object types (in file)
array_of_array_of_object_required_in_file: readonly ObjectType[][];
// Nested array of array of object types (with spread)
array_of_array_of_object_required_with_spread: readonly Readonly<ObjectType & {}>[][];
}
export default codegenNativeComponent<ModuleProps>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1221,6 +1221,29 @@ exports[`RN Codegen TypeScript Parser can generate fixture ARRAY_PROP_TYPES_NO_E
}
}
}
},
{
'name': 'array_of_array_of_object_required_with_spread',
'optional': false,
'typeAnnotation': {
'type': 'ArrayTypeAnnotation',
'elementType': {
'type': 'ArrayTypeAnnotation',
'elementType': {
'type': 'ObjectTypeAnnotation',
'properties': [
{
'name': 'prop',
'optional': false,
'typeAnnotation': {
'type': 'StringTypeAnnotation',
'default': null
}
}
]
}
}
}
}
],
'commands': []
Expand Down Expand Up @@ -1905,6 +1928,29 @@ exports[`RN Codegen TypeScript Parser can generate fixture ARRAY2_PROP_TYPES_NO_
}
}
}
},
{
'name': 'array_of_array_of_object_required_with_spread',
'optional': false,
'typeAnnotation': {
'type': 'ArrayTypeAnnotation',
'elementType': {
'type': 'ArrayTypeAnnotation',
'elementType': {
'type': 'ObjectTypeAnnotation',
'properties': [
{
'name': 'prop',
'optional': false,
'typeAnnotation': {
'type': 'StringTypeAnnotation',
'default': null
}
}
]
}
}
}
}
],
'commands': []
Expand Down
Loading

0 comments on commit 813fd04

Please sign in to comment.