Skip to content

Commit

Permalink
feat: support "null to optional"
Browse files Browse the repository at this point in the history
  • Loading branch information
innocenzi committed Oct 9, 2024
1 parent 6e2d28d commit 28d9b1f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,13 @@ function (string $carry, ReflectionProperty $property) use ($isOptional, $dataCl
fn (object $attribute) => $attribute instanceof TypeScriptOptional
)
|| ($dataProperty->type->lazyType && $dataProperty->type->lazyType !== ClosureLazy::class)
|| $dataProperty->type->isOptional;
|| $dataProperty->type->isOptional
|| ($dataProperty->type->isNullable && $this->config->shouldConsiderNullAsOptional());

$transformed = $this->typeToTypeScript(
$type,
$missingSymbols,
$this->config->shouldConsiderNullAsOptional(),
currentClass: $property->getDeclaringClass()->getName(),
);

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

it('supports converting nullable types to optional properties', function () {
$config = TypeScriptTransformerConfig::create();
$config->nullToOptional(true);

$data = new class ('foo') extends Data {
public function __construct(
public ?string $nullable,
) {
}
};

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

expect($transformer->canTransform($reflection))->toBeTrue();
assertMatchesSnapshot($transformer->transform($reflection, 'DataObject')->transformed);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
nullable?: string;
}

0 comments on commit 28d9b1f

Please sign in to comment.