diff --git a/src/Support/TypeScriptTransformer/DataTypeScriptTransformer.php b/src/Support/TypeScriptTransformer/DataTypeScriptTransformer.php index 4aad0b8c..4fb8cc5e 100644 --- a/src/Support/TypeScriptTransformer/DataTypeScriptTransformer.php +++ b/src/Support/TypeScriptTransformer/DataTypeScriptTransformer.php @@ -81,6 +81,10 @@ function (string $carry, ReflectionProperty $property) use ($isOptional, $dataCl $propertyName = $dataProperty->outputMappedName ?? $dataProperty->name; + if (! preg_match('/^[$_a-zA-Z][$_a-zA-Z0-9]*$/', $propertyName)) { + $propertyName = "'{$propertyName}'"; + } + return $isOptional ? "{$carry}{$propertyName}?: {$transformed};" . PHP_EOL : "{$carry}{$propertyName}: {$transformed};" . PHP_EOL; diff --git a/tests/Support/TypeScriptTransformer/DataTypeScriptTransformerTest.php b/tests/Support/TypeScriptTransformer/DataTypeScriptTransformerTest.php index 0a37c7fd..901f4a75 100644 --- a/tests/Support/TypeScriptTransformer/DataTypeScriptTransformerTest.php +++ b/tests/Support/TypeScriptTransformer/DataTypeScriptTransformerTest.php @@ -160,10 +160,12 @@ public function __construct( it('outputs types with properties using their mapped name', function () { $config = TypeScriptTransformerConfig::create(); - $data = new class ('Good job Ruben') extends Data { + $data = new class ('Good job Ruben', 'Hi Ruben') extends Data { public function __construct( #[MapOutputName(SnakeCaseMapper::class)] public string $someCamelCaseProperty, + #[MapOutputName('some:non:standard:property')] + public string $someNonStandardProperty, ) { } }; diff --git a/tests/__snapshots__/DataTypeScriptTransformerTest__it_outputs_types_with_properties_using_their_mapped_name__1.txt b/tests/__snapshots__/DataTypeScriptTransformerTest__it_outputs_types_with_properties_using_their_mapped_name__1.txt index d6b17f4a..e5b50a34 100644 --- a/tests/__snapshots__/DataTypeScriptTransformerTest__it_outputs_types_with_properties_using_their_mapped_name__1.txt +++ b/tests/__snapshots__/DataTypeScriptTransformerTest__it_outputs_types_with_properties_using_their_mapped_name__1.txt @@ -1,3 +1,4 @@ { some_camel_case_property: string; +'some:non:standard:property': string; } \ No newline at end of file