spatie/typescript-transformer

Nullable property types

Closed this issue · 2 comments

Although we have a concept of null in property types, we don't have a concept for nullable property types like:

name ?: string;

@rubenvanassche what was the reason this was closed? I can't find a method to automatically convert a Data class with optional properties without using Lazy. I would like to convert the following object

class Test extends Data
{
    public function __construct(
        public string $description_1,
        public ?string $description_2,
        public ?string $description_3 = null,
    ) {
    }
}

to

export type Department = {
    description_1: string;
    description_2: string|null;
    description_3?: string|null;
}

Is this possible or do I need a custom solution for this?

Hi @iamrgroot,

The reason for closing this is, how do we know if a property can be null or if a property is nullable. In code, how do we make a distinction between these two constructs in PHP:

export type Department = {
    a: string|null;
    b?: string
;}

I think attributes might be a solution here. So if you create a PR that implements this feature I would certainly merge it!