Skip to content

Commit 6c9b650

Browse files
committed
Handle array with union type inside
1 parent 4c4c310 commit 6c9b650

File tree

5 files changed

+17
-1
lines changed

5 files changed

+17
-1
lines changed

src/Ast/PhpDocTypeParserTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ public function getData(): array
8383
),
8484
),
8585
],
86+
[
87+
'array with union inside',
88+
'/** @var (int|string)[] */',
89+
new PhpListType(new PhpUnionType([
90+
PhpBaseType::int(),
91+
PhpBaseType::string(),
92+
]),),
93+
],
8694
[
8795
'@var is required',
8896
'/** int[]|null */',

src/Language/TypeScript/TypeScriptGenerator.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,11 @@ private function getTypeScriptTypeFromPhp(PhpTypeInterface $type, DtoType|null $
193193
}
194194

195195
if ($type instanceof PhpListType) {
196-
return sprintf('%s[]', $this->getTypeScriptTypeFromPhp($type->getType(), $dto, $dtoList));
196+
$listType = $this->getTypeScriptTypeFromPhp($type->getType(), $dto, $dtoList);
197+
if ($type->getType() instanceof PhpUnionType) {
198+
return sprintf('(%s)[]', $listType);
199+
}
200+
return sprintf('%s[]', $listType);
197201
}
198202

199203
if ($type instanceof PhpBaseType) {

tests/TypeScriptGeneratorTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,8 @@ class UserCreateInput
295295
public Money $money;
296296
public GenderEnum $gender;
297297
public LocationEmbeddable $location;
298+
/** @var (int|string|null)[] */
299+
public array $mixedArray;
298300
}
299301

300302
CODE;

tests/__snapshots__/TypeScriptGeneratorTest__testApiPlatformInput__1.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ export type UserCreateInput = {
2424
money: { currency: string; amount: number };
2525
gender: { value: GenderEnum };
2626
location: { lat: string; lan: string };
27+
mixedArray: (number | string | null)[];
2728
};

tests/__snapshots__/TypeScriptGeneratorTest__testApiPlatformInput__2.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ export type UserCreateInput = {
2424
money: { currency: string; amount: number };
2525
gender: { value: GenderEnum };
2626
location: { lat: string; lan: string };
27+
mixedArray: (number | string | null)[];
2728
};

0 commit comments

Comments
 (0)