Skip to content

Commit deedec5

Browse files
Merge pull request #54 from adrum/feature/hidden-attribute
Hidden attribute
2 parents 93e2cc4 + 58f1c72 commit deedec5

File tree

5 files changed

+62
-0
lines changed

5 files changed

+62
-0
lines changed

docs/dtos/typing-properties.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,3 +252,28 @@ Now all properties will be optional:
252252
name? : string;
253253
}
254254
```
255+
256+
## Hidden types
257+
258+
You can make certain properties of a DTO hidden in TypeScript as such:
259+
260+
```php
261+
class DataObject extends Data
262+
{
263+
public function __construct(
264+
public int $id,
265+
#[Hidden]
266+
public string $hidden,
267+
)
268+
{
269+
}
270+
}
271+
```
272+
273+
This will be transformed into:
274+
275+
```tsx
276+
{
277+
id : number;
278+
}
279+
```

src/Attributes/Hidden.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Spatie\TypeScriptTransformer\Attributes;
4+
5+
use Attribute;
6+
7+
#[Attribute(Attribute::TARGET_PROPERTY)]
8+
class Hidden
9+
{
10+
}

src/Transformers/DtoTransformer.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use ReflectionClass;
66
use ReflectionProperty;
7+
use Spatie\TypeScriptTransformer\Attributes\Hidden;
78
use Spatie\TypeScriptTransformer\Attributes\Optional;
89
use Spatie\TypeScriptTransformer\Structures\MissingSymbolsCollection;
910
use Spatie\TypeScriptTransformer\Structures\TransformedType;
@@ -58,6 +59,12 @@ protected function transformProperties(
5859
return array_reduce(
5960
$this->resolveProperties($class),
6061
function (string $carry, ReflectionProperty $property) use ($isOptional, $missingSymbols) {
62+
$isHidden = ! empty($property->getAttributes(Hidden::class));
63+
64+
if ($isHidden) {
65+
return $carry;
66+
}
67+
6168
$isOptional = $isOptional || ! empty($property->getAttributes(Optional::class));
6269

6370
$transformed = $this->reflectionToTypeScript(

tests/Transformers/DtoTransformerTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use function Spatie\Snapshots\assertMatchesSnapshot;
77
use function Spatie\Snapshots\assertMatchesTextSnapshot;
88
use Spatie\TypeScriptTransformer\Attributes\LiteralTypeScriptType;
9+
use Spatie\TypeScriptTransformer\Attributes\Hidden;
910
use Spatie\TypeScriptTransformer\Attributes\Optional;
1011
use Spatie\TypeScriptTransformer\Attributes\TypeScriptType;
1112
use Spatie\TypeScriptTransformer\Structures\MissingSymbolsCollection;
@@ -129,3 +130,19 @@ class DummyOptionalDto
129130

130131
assertMatchesSnapshot($type->transformed);
131132
});
133+
134+
135+
it('transforms properties to hidden ones when using hidden attribute', function () {
136+
$class = new class () {
137+
public string $visible;
138+
#[Hidden]
139+
public string $hidden;
140+
};
141+
142+
$type = $this->transformer->transform(
143+
new ReflectionClass($class),
144+
'Typed'
145+
);
146+
147+
assertMatchesSnapshot($type->transformed);
148+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
visible: string;
3+
}

0 commit comments

Comments
 (0)