question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Non nullable properties are generated as possibly undefined

See original GitHub issue

I have an object of a product that has some non nullable properties such as ProductId and price with the following schema generated by swagger

"ProductBasicInfo": {
        "type": "object",
        "properties": {
          "productId": {
            "type": "integer",
            "format": "int32"
          },
          "brand": {
            "type": "string",
            "nullable": true
          },
          "description": {
            "type": "string",
            "nullable": true
          },
          "name": {
            "type": "string",
            "nullable": true
          },
          "price": {
            "type": "number",
            "format": "double"
          }
        },
        "additionalProperties": false
      }

This is the generated contract

export interface ProductBasicInfo {
  /** @format int32 */
  productId?: number;
  brand?: string | null;
  description?: string | null;
  name?: string | null;

  /** @format double */
  price?: number;
}

As you can see, in the generated contract a ? was added to the non nullable properties making their type effectively number | undefined

Is it possible to omit the ? mark for non nullable types?

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:6
  • Comments:8

github_iconTop GitHub Comments

1reaction
HansFalkenberg-Vismacommented, Sep 27, 2022

What @remkoboschker said is correct. The issue here seems to be the API specification, not this generator.

Unless the name of an OpenApi property is included in the object’s required list, it is an optional property. Optional properties are expressed with ? or undefined in TypeScript.

Nullability is something completely different, namely whether the value of a property that is present may be null. Expressed as null in TypeScript.

I would also recommend simply never using null in TypeScript. The scenarios where it is truly needed are few and far between and the language support for undefined is much better. This works out if any C#/Java implementations – which don’t have a native concept of undefined – treat missing properties as null. If you use both null and undefined in TypeScript and then expect those to be different things (which they totally are!), it likely won’t work out when you meet a C#/Java implementation.

1reaction
remkoboschkercommented, Sep 16, 2022

I think the issues is that the open-api spec states that nullable true is a way to say that a property may have the value null. To express that a property may not be undefined would require listing the property as required for that object.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to suppress "error TS2533: Object is possibly 'null' or ...
If you know from external means that an expression is not null or undefined , you can use the non-null assertion operator !...
Read more >
Documentation - TypeScript 2.0
Null - and undefined-aware types. TypeScript has two special types, Null and Undefined, that have the values null and undefined respectively.
Read more >
3 Simple Ways To Fix Object Is Possibly Undefined In TypeScript
This video explains how to fix "object is possibly undefined or null " in TypeScript.Object is possibly null in TypeScript: ...
Read more >
Null and undefined (Reference) - Prisma
In this instance, the value of the email property is undefined . When this query is run no data is returned. This is...
Read more >
Non-Nullable Types in TypeScript - Marius Schulz
#The null and undefined Values ... The same was true for objects, array, and function types. There was no way to express via...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found