Skip to content

Commit 33d1291

Browse files
ZihanChen-MSFTfacebook-github-bot
authored andcommitted
Support TypeScript array types for turbo module (component only) (#34216)
Summary: Turbo module codegen for component parse `readonly T[]` and `readonly Object[][]` incorrectly. In this change, it is fixed, with necessary test cases added. ## Changelog [General] [Added] - Support TypeScript array types for turbo module (component only) Pull Request resolved: #34216 Test Plan: `yarn jest` passed in `packages/react-native-codegen` Reviewed By: sammy-SC Differential Revision: D37953004 Pulled By: cipolleschi fbshipit-source-id: b71ea35eb9c4bd5dc41130f1dcf9201704ec298e
1 parent 2013a20 commit 33d1291

File tree

3 files changed

+872
-29
lines changed

3 files changed

+872
-29
lines changed

packages/react-native-codegen/src/parsers/typescript/components/__test_fixtures__/fixtures.js

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,138 @@ export default codegenNativeComponent<ModuleProps>(
459459
) as HostComponent<ModuleProps>;
460460
`;
461461

462+
const ARRAY2_PROP_TYPES_NO_EVENTS = `
463+
/**
464+
* Copyright (c) Meta Platforms, Inc. and affiliates.
465+
*
466+
* This source code is licensed under the MIT license found in the
467+
* LICENSE file in the root directory of this source tree.
468+
*
469+
* @format
470+
*/
471+
472+
'use strict';
473+
474+
const codegenNativeComponent = require('codegenNativeComponent');
475+
476+
import type {Int32, Double, Float, WithDefault} from 'CodegenTypes';
477+
import type {ImageSource} from 'ImageSource';
478+
import type {
479+
ColorValue,
480+
ColorArrayValue,
481+
PointValue,
482+
EdgeInsetsValue,
483+
} from 'StyleSheetTypes';
484+
import type {ViewProps} from 'ViewPropTypes';
485+
import type {HostComponent} from 'react-native';
486+
487+
type ObjectType = Readonly<{prop: string}>;
488+
type ArrayObjectType = readonly Readonly<{prop: string}>[];
489+
490+
export interface ModuleProps extends ViewProps {
491+
// Props
492+
// Boolean props
493+
array_boolean_required: readonly boolean[];
494+
array_boolean_optional_key?: readonly boolean[];
495+
array_boolean_optional_value: readonly boolean[] | null | undefined;
496+
array_boolean_optional_both?: readonly boolean[] | null | undefined;
497+
498+
// String props
499+
array_string_required: readonly string[];
500+
array_string_optional_key?: readonly string[];
501+
array_string_optional_value: readonly string[] | null | undefined;
502+
array_string_optional_both?: readonly string[] | null | undefined;
503+
504+
// Double props
505+
array_double_required: readonly Double[];
506+
array_double_optional_key?: readonly Double[];
507+
array_double_optional_value: readonly Double[] | null | undefined;
508+
array_double_optional_both?: readonly Double[] | null | undefined;
509+
510+
// Float props
511+
array_float_required: readonly Float[];
512+
array_float_optional_key?: readonly Float[];
513+
array_float_optional_value: readonly Float[] | null | undefined;
514+
array_float_optional_both?: readonly Float[] | null | undefined;
515+
516+
// Int32 props
517+
array_int32_required: readonly Int32[];
518+
array_int32_optional_key?: readonly Int32[];
519+
array_int32_optional_value: readonly Int32[] | null | undefined;
520+
array_int32_optional_both?: readonly Int32[] | null | undefined;
521+
522+
// String enum props
523+
array_enum_optional_key?: WithDefault<
524+
readonly ('small' | 'large')[],
525+
'small'
526+
>;
527+
array_enum_optional_both?: WithDefault<
528+
readonly ('small' | 'large')[],
529+
'small'
530+
>;
531+
532+
// ImageSource props
533+
array_image_required: readonly ImageSource[];
534+
array_image_optional_key?: readonly ImageSource[];
535+
array_image_optional_value: readonly ImageSource[] | null | undefined;
536+
array_image_optional_both?: readonly ImageSource[] | null | undefined;
537+
538+
// ColorValue props
539+
array_color_required: readonly ColorValue[];
540+
array_color_optional_key?: readonly ColorValue[];
541+
array_color_optional_value: readonly ColorValue[] | null | undefined;
542+
array_color_optional_both?: readonly ColorValue[] | null | undefined;
543+
544+
// PointValue props
545+
array_point_required: readonly PointValue[];
546+
array_point_optional_key?: readonly PointValue[];
547+
array_point_optional_value: readonly PointValue[] | null | undefined;
548+
array_point_optional_both?: readonly PointValue[] | null | undefined;
549+
550+
// EdgeInsetsValue props
551+
array_insets_required: readonly EdgeInsetsValue[];
552+
array_insets_optional_key?: readonly EdgeInsetsValue[];
553+
array_insets_optional_value: readonly EdgeInsetsValue[] | null | undefined;
554+
array_insets_optional_both?: readonly EdgeInsetsValue[] | null | undefined;
555+
556+
// Object props
557+
array_object_required: readonly Readonly<{prop: string}>[];
558+
array_object_optional_key?: readonly Readonly<{prop: string}>[];
559+
array_object_optional_value: ArrayObjectType | null | undefined;
560+
array_object_optional_both?: readonly ObjectType[] | null | undefined;
561+
562+
// Nested array object types
563+
array_of_array_object_required: readonly Readonly<{
564+
// This needs to be the same name as the top level array above
565+
array_object_required: readonly Readonly<{prop: string}>[];
566+
}>[];
567+
array_of_array_object_optional_key?: readonly Readonly<{
568+
// This needs to be the same name as the top level array above
569+
array_object_optional_key: readonly Readonly<{prop?: string}>[];
570+
}>[];
571+
array_of_array_object_optional_value: readonly Readonly<{
572+
// This needs to be the same name as the top level array above
573+
array_object_optional_value: readonly Readonly<{prop: string | null | undefined}>[];
574+
}>[] | null | undefined;
575+
array_of_array_object_optional_both?: readonly Readonly<{
576+
// This needs to be the same name as the top level array above
577+
array_object_optional_both: readonly Readonly<{prop?: string | null | undefined}>[];
578+
}>[] | null | undefined;
579+
580+
// Nested array of array of object types
581+
array_of_array_of_object_required: readonly Readonly<{
582+
prop: string;
583+
}>[][];
584+
585+
// Nested array of array of object types (in file)
586+
array_of_array_of_object_required_in_file: readonly ObjectType[][];
587+
}
588+
589+
export default codegenNativeComponent<ModuleProps>(
590+
'Module',
591+
) as HostComponent<ModuleProps>;
592+
`;
593+
462594
const OBJECT_PROP_TYPES_NO_EVENTS = `
463595
/**
464596
* Copyright (c) Meta Platforms, Inc. and affiliates.
@@ -959,6 +1091,7 @@ export default codegenNativeComponent<ModuleProps>(
9591091
module.exports = {
9601092
ALL_PROP_TYPES_NO_EVENTS,
9611093
ARRAY_PROP_TYPES_NO_EVENTS,
1094+
ARRAY2_PROP_TYPES_NO_EVENTS,
9621095
OBJECT_PROP_TYPES_NO_EVENTS,
9631096
PROPS_ALIASED_LOCALLY,
9641097
ONE_OF_EACH_PROP_EVENT_DEFAULT_AND_OPTIONS,

0 commit comments

Comments
 (0)