Manweill / swagger-axios-codegen

swagger client to use axios and typescript
MIT License
306 stars 83 forks source link

How to generate an interface with properties that are nullable #124

Closed pbn4 closed 3 years ago

pbn4 commented 3 years ago

I would like to have an interface with properties that are a union of a specific type and a null instead of '?' (undefined), e.g.:

export interface A {
prop: string | null 
}

instead of:

export interface A {
prop?: string
}

is there a way to achieve that with this library?

Manweill commented 3 years ago

same with these example

pbn4 commented 3 years ago

@Manweill They are not the same when strict null check is enabled. So is there a way to generate this 1st kind of interface I specified?

pbn4 commented 3 years ago

I see that here https://github.com/Manweill/swagger-axios-codegen/blob/master/src/templates/template.ts#L44 is a check for nullable prop but an optional operator is always used. What do you think about adding optional operator based on required and | null union based on "nullable"?

Manweill commented 3 years ago

set codegen options.strictNullCheck=true

Manweill commented 3 years ago
let obj: {
  a: number | null | undefined;
  b?: number | null | undefined;
} = {

}

image

@pbn4 There is only one difference between the two writing methods. When initializing obj, a must be defined, b is not

Manweill commented 3 years ago

Long time is not active!