Skip to main content

status becomes Statu. first-name keeps the hyphen

The converter drops one trailing s from list keys. A hyphen key is copied into TypeScript and the PHP DTO.

Updated 2 min readMarkdown
status becomes Statu. first-name keeps the hyphen

status 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.

export interface Root {
  status: Statu[];
}

export interface Statu {
  code: string;
}

That is the TypeScript draft for {"status":[{"code":"ok"}]} from the JSON converter. The PHP DTO types the same field as public array $status. It does not create a class named Statu. Conversion runs in the browser.

A hyphen is copied, not cleaned

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:

public string $first-name;

The constructor line is $this->first-name = $data['first-name'] ?? null. The class name stays DataTransferObject. A nested object on that class is array, not a second class. A missing key is still assigned with ?? null, including on a property declared public int $id.

Treat the output as a draft

Rename the class before it ships. Replace a hyphen key with a legal identifier. Check child interface names that came from plural keys. If the paste needs repair first, use the JSON formatter. A trailing comma clears every format.

Guide: https://kepttools.com/guides/json-to-typescript-php-dto-laravel

No comments yet. Be the first to comment.