diff --git a/src/Actions/TranspileTypeToTypeScriptAction.php b/src/Actions/TranspileTypeToTypeScriptAction.php index 448e6f09..013d2e55 100644 --- a/src/Actions/TranspileTypeToTypeScriptAction.php +++ b/src/Actions/TranspileTypeToTypeScriptAction.php @@ -4,6 +4,7 @@ use Exception; use phpDocumentor\Reflection\PseudoType; +use phpDocumentor\Reflection\PseudoTypes\ArrayShape; use phpDocumentor\Reflection\Type; use phpDocumentor\Reflection\Types\AbstractList; use phpDocumentor\Reflection\Types\Boolean; @@ -51,6 +52,7 @@ public function execute(Type $type): string $type instanceof AbstractList => $this->resolveListType($type), $type instanceof Nullable => $this->resolveNullableType($type), $type instanceof Object_ => $this->resolveObjectType($type), + $type instanceof ArrayShape => $this->resolveArrayShapeType($type), $type instanceof StructType => $this->resolveStructType($type), $type instanceof RecordType => $this->resolveRecordType($type), $type instanceof TypeScriptType => (string) $type, @@ -106,6 +108,18 @@ private function resolveObjectType(Object_ $object): string ); } + private function resolveArrayShapeType(ArrayShape $type): string + { + $transformed = "{"; + + foreach ($type->getItems() as $type) { + $q = $type->isOptional() ? '?' : ''; + $transformed .= "{$type->getKey()}{$q}:{$this->execute($type->getValue())};"; + } + + return "{$transformed}}"; + } + private function resolveStructType(StructType $type): string { $transformed = "{"; @@ -133,6 +147,10 @@ private function resolveSelfReferenceType(): string private function isTypeScriptArray(Type $keyType): bool { + if ($keyType instanceof Integer) { + return true; + } + if (! $keyType instanceof Compound) { return false; } diff --git a/src/TypeReflectors/PropertyTypeReflector.php b/src/TypeReflectors/PropertyTypeReflector.php index be7a16da..3e947410 100644 --- a/src/TypeReflectors/PropertyTypeReflector.php +++ b/src/TypeReflectors/PropertyTypeReflector.php @@ -24,7 +24,7 @@ protected function getDocblock(): string protected function docblockRegex(): string { - return '/@var ((?:\s?[\\w?|\\\\<>,-]+(?:\[])?)+)/'; + return '/@var ((?:\s?[\\w?|\\\\<>,-\{\}]+(?:\[])?)+)/'; } protected function getReflectionType(): ?ReflectionType