Skip to main content

status becomes the interface Statu

The converter drops one trailing s, then Pascal-cases the key. items becomes Item. A hyphen key is copied into the TypeScript field and the PHP property.

Updated 2 min readMarkdown
status becomes the interface Statu

A list of statuses does not become an interface named Status. The converter drops one trailing s from the key, then Pascal-cases what is left. status becomes Statu. items becomes Item.

This input is {"status":[{"code":"ok"}]}.

export interface Root {
  status: Statu[];
}

export interface Statu {
  code: string;
}

And {"items":[{"id":1}]} is a different child name:

export interface Root {
  items: Item[];
}

export interface Item {
  id: number;
}

The PHP DTO does not create those interfaces. Both properties are array. For the status object the property is public array $status, and the constructor line is $this->status = $data['status'] ?? null.

A hyphen survives in the other two drafts

Laravel can quote a hyphen. For {"first-name":"Ada","role":"engineer"} the array entry is 'first-name' => 'Ada'.

TypeScript writes the key as it arrived:

export interface Root {
  first-name: string;
  role: string;
}

The PHP DTO does the same with the property:

public string $first-name;

The constructor copies it with $this->first-name = $data['first-name'] ?? null. The class around that line is still named DataTransferObject. That name does not change. A nested object on the same class is array, not a second class, and a missing key is assigned with ?? null even when the property is public int $id.

Rename the class before it ships, and replace a hyphen key with a name the language accepts. The JSON converter only drafts the text. Conversion runs in the browser.

The first-object limit, the CSV cell, and the parse errors are in the guide: https://kepttools.com/guides/json-to-typescript-php-dto-laravel

No comments yet. Be the first to comment.