How do I set response models to not be optional as typescript is saying a value can be undefined when it can never be the case.
See original GitHub issueI’m using fast endpoints with a typescript client automatically generated from the Open API specification generated by Fast Endpoints.
I have response models which I know they will always have a value, however, currently the response models are being created with all fields being marked as nullable, for example:
id?: string;
name?: string;
headerImage?: string;
my understanding is that this is happening because the Open API definition is created as per the below:
"schemas": {
"Response": {
"type": "object",
"additionalProperties": false,
"properties": {
"id": {
"type": "string"
},
"name": {
"type": "string"
},
"headerImage": {
"type": "string"
}
}
},
I’d like to generate this definition instead:
"schemas": {
"Response": {
"type": "object",
"required": [
"id",
"name",
],
"additionalProperties": false,
"properties": {
"id": {
"type": "string"
},
"name": {
"type": "string"
},
"headerImage": {
"type": "string"
}
}
},
Is it possible to create this OpenAPI definition?
Issue Analytics
- State:
- Created 7 months ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
How to Deal with Optional Things and "Undefined" in TypeScript
First, if you don't tell TypeScript that a property is optional, it will expect it to be set. TypeScript. type Foo = {...
Read more >Why a non-optional variable can be undefined in this case
if I create a non-optional variable of type "data". Why the value in the next case can be undefined and how can I...
Read more >Documentation - Do's and Don'ts
Don't ever use the types Number , String , Boolean , Symbol , or Object These ... Don't use the return type any...
Read more >When to use typescript optional "?" parameter vs explicit ...
Undefined means that parameter must be passed in but its value may be undefined.
Read more >How To Fix Object is Possibly Undefined In TypeScript
Non-null assertions can be used to avoid the "object is possibly undefined" error by explicitly telling TypeScript that a variable or property ...
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 FreeTop 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
Top GitHub Comments
added an extension method in
5.8.1.14-beta
Thanks for the update. I can confirm that the code above is a valid workaround, however, I think there will be some cases where this wouldn’t work, particularly if you don’t enable the nullable reference types feature of C#8. From what I can gather, the IsNullable function listed above doesn’t return true for arrays, strings or reference types which means that unless you enable nullable reference types this functionality will probably cause issues down the line in clients. I will do some further investigation and will update the issue with any information.