Non nullable properties are generated as possibly undefined
See original GitHub issueI 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:
- Created a year ago
- Reactions:6
- Comments:8
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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?
orundefined
in TypeScript.Nullability is something completely different, namely whether the value of a property that is present may be
null
. Expressed asnull
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 forundefined
is much better. This works out if any C#/Java implementations – which don’t have a native concept ofundefined
– treat missing properties asnull
. If you use bothnull
andundefined
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.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.