DEV Community

Cover image for null becomes mixed on the PHP DTO
Ameen Usman
Ameen Usman

Posted on

null becomes mixed on the PHP DTO

I pasted nulls as property values and expected the same type everywhere. On the JSON converter they stay null in the paste, but the drafts diverge. Conversion runs in the browser. Nothing is uploaded.

The sample shape

{"name":null,"age":null,"meta":null}
Enter fullscreen mode Exit fullscreen mode

Laravel writes null for each key. TypeScript types each field as null. The PHP DTO types each property as mixed. CSV writes the headers name,age,meta with an empty row underneath.

That is different from a nested object. When meta is {"active":true}, TypeScript drafts interface Meta and the PHP DTO still uses public array $meta. A null meta does not create that nest. It is mixed on the PHP class and null in TypeScript.

Missing keys are a different path

A missing key on the PHP DTO is still assigned with ?? null, including on a property declared public int $id. That is an assignment fallback. A JSON null value is an explicit null in the paste, and the draft types that property as mixed on the PHP class.

The class name stays DataTransferObject. Rename it before the draft ships.

Keep parse errors separate

A trailing comma such as {"a":1,} clears every format with Expected double-quoted property name in JSON at position 7 (line 1 column 8). That is not a null. Repair broken text in the JSON formatter, then convert again.

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

Top comments (0)