Skip to content

Add LiteralTypeScriptExtraTypes attribute for enhanced TypeScript type definitions #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions docs/usage/annotations.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,44 @@ export type FleetData = {
}
```

## Adding additional type information

Sometimes you need to add additional type information to your TypeScript types, especially when working with specifications like JSON:API. You can use the `LiteralTypeScriptExtraTypes` attribute to add additional properties to your TypeScript types:

```php
use Spatie\TypeScriptTransformer\Attributes\LiteralTypeScriptExtraTypes;

#[TypeScript]
#[LiteralTypeScriptExtraTypes([
'type' => "'users'",
])]
class UserData {
public string $name;
}
```

This will generate the following TypeScript:

```ts
export type UserData = {
type: 'users';
name: string;
};
```

You can add as many additional properties as you need:

```php
#[TypeScript]
#[LiteralTypeScriptExtraTypes([
'type' => "'users'",
'links' => '{self: string}',
])]
class UserData {
public string $name;
}
```

## Selecting a transformer

Want to define a specific transformer for the file? You can use the following annotation:
Expand Down
21 changes: 21 additions & 0 deletions src/Attributes/LiteralTypeScriptExtraTypes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Spatie\TypeScriptTransformer\Attributes;

use Attribute;

#[Attribute(Attribute::TARGET_CLASS)]
class LiteralTypeScriptExtraTypes
{
private array $extras;

public function __construct(array $extras)
{
$this->extras = $extras;
}

public function getExtras(): array
{
return $this->extras;
}
}
23 changes: 23 additions & 0 deletions src/Transformers/DtoTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use ReflectionProperty;
use Spatie\TypeScriptTransformer\Attributes\Hidden;
use Spatie\TypeScriptTransformer\Attributes\Optional;
use Spatie\TypeScriptTransformer\Attributes\LiteralTypeScriptExtraTypes;
use Spatie\TypeScriptTransformer\Structures\MissingSymbolsCollection;
use Spatie\TypeScriptTransformer\Structures\TransformedType;
use Spatie\TypeScriptTransformer\TypeProcessors\DtoCollectionTypeProcessor;
Expand Down Expand Up @@ -35,6 +36,7 @@ public function transform(ReflectionClass $class, string $name): ?TransformedTyp
$this->transformProperties($class, $missingSymbols),
$this->transformMethods($class, $missingSymbols),
$this->transformExtra($class, $missingSymbols),
$this->transformLiteralTypeScriptExtraTypes($class, $missingSymbols),
]);

return TransformedType::create(
Expand Down Expand Up @@ -105,6 +107,27 @@ protected function transformExtra(
return '';
}

protected function transformLiteralTypeScriptExtraTypes(
ReflectionClass $class,
MissingSymbolsCollection $missingSymbols
): string {
$attributes = $class->getAttributes(LiteralTypeScriptExtraTypes::class);

if (empty($attributes)) {
return '';
}

$attribute = $attributes[0]->newInstance();
$extras = $attribute->getExtras();

$result = '';
foreach ($extras as $key => $value) {
$result .= "{$key}: {$value};" . PHP_EOL;
}

return $result;
}

protected function transformPropertyName(
ReflectionProperty $property,
MissingSymbolsCollection $missingSymbols
Expand Down
36 changes: 36 additions & 0 deletions tests/Transformers/DtoTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Spatie\TypeScriptTransformer\Attributes\Hidden;
use Spatie\TypeScriptTransformer\Attributes\LiteralTypeScriptType;
use Spatie\TypeScriptTransformer\Attributes\Optional;
use Spatie\TypeScriptTransformer\Attributes\LiteralTypeScriptExtraTypes;
use Spatie\TypeScriptTransformer\Attributes\TypeScriptType;
use Spatie\TypeScriptTransformer\Structures\MissingSymbolsCollection;
use Spatie\TypeScriptTransformer\Tests\FakeClasses\Enum\RegularEnum;
Expand Down Expand Up @@ -161,3 +162,38 @@ class DummyOptionalDto

$this->assertMatchesSnapshot($type->transformed);
});

it('adds extra type information when using LiteralTypeScriptExtraTypes attribute', function () {
#[LiteralTypeScriptExtraTypes([
'type' => "'users'",
])]
class DummyUserData
{
public string $name;
}

$type = $this->transformer->transform(
new ReflectionClass(DummyUserData::class),
'Typed'
);

assertMatchesSnapshot($type->transformed);
});

it('adds multiple extra type information when using LiteralTypeScriptExtraTypes attribute', function () {
#[LiteralTypeScriptExtraTypes([
'type' => "'users'",
'links' => '{self: string}',
])]
class DummyUserDataWithLinks
{
public string $name;
}

$type = $this->transformer->transform(
new ReflectionClass(DummyUserDataWithLinks::class),
'Typed'
);

assertMatchesSnapshot($type->transformed);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
name: string;
type: 'users';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
name: string;
type: 'users';
links: {self: string};
}