Skip to content

Commit

Permalink
Fix ts transformed collections
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvanassche committed Apr 4, 2024
1 parent f01f389 commit 0d77a24
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ protected function dataCollectionType(string $class, ?string $keyType): Type
{
$keyType = match ($keyType) {
'string' => new String_(),
'int' => new Integer(),
'int' => null,
default => new Compound([new String_(), new Integer()]),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,25 @@ public function __construct(
$transformer->transform($reflection, 'DataObject')->transformed
);
});

it('will transform a collection with int key as an array', function () {
$config = TypeScriptTransformerConfig::create();

$data = new class () extends Data {
/** @var array<int, \Spatie\LaravelData\Tests\Fakes\SimpleData> */
public array $collection;
};

$transformer = new DataTypeScriptTransformer($config);
$reflection = new ReflectionClass($data);

$this->assertTrue($transformer->canTransform($reflection));
$this->assertEquals(
<<<TXT
{
collection: Array<{%Spatie\LaravelData\Tests\Fakes\SimpleData%}>;
}
TXT,
$transformer->transform($reflection, 'DataObject')->transformed
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
some_camel_case_property: string;
'some:non:standard:property': string;
}

0 comments on commit 0d77a24

Please sign in to comment.