Non-nullable properties are marked as optional in generated TypeScript
See original GitHub issueI have the YAML below. Two properties are explicit nullable: true
(completedBy
, completedTime
) while the others are implicit nullable: false
. When generating a TypeScript client I get the following interface:
export interface ITask {
plantId?: string;
hoursInterval?: number;
deadline?: Date;
completedBy?: string | null;
completedTime?: Date | null;
taskId?: string;
}
I would expect all properties to not have the ?
as they are not nullable. Except of course completedBy
and completedTime
.
openapi: 3.0.1
info:
title: Non-nullable demo
description: Non-nullable properties are treated as nullable in generated TypeScript
version: v1
servers:
- url: /v1
description: Api version 1.0
paths:
'/tasks':
get:
summary: ''
tags: []
operationId: getTasks
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/Tasks'
components:
schemas:
Tasks:
description: A list of tasks.
type: array
items:
$ref: '#/components/schemas/Task'
x-tags:
- Tasks
title: Tasks
Task:
title: Task
type: object
description: A task.
properties:
plantId:
type: string
hoursInterval:
type: integer
deadline:
type: string
format: date-time
completedBy:
nullable: true
type: string
completedTime:
nullable: true
type: string
format: date-time
taskId:
type: string
Issue Analytics
- State:
- Created 3 years ago
- Reactions:12
- Comments:26 (3 by maintainers)
Top Results From Across the Web
How to make nullable properties optional in TypeScript?
I created two utility types which pick all nullable not non-nullable properties from a type respectively. We can use these to construct a ......
Read more >Making optional properties nullable in TypeScript - rbardini.com
Your types are defined with non-nullable optional properties (e.g., x?: number ), but the data coming from the API returns null instead.
Read more >Documentation - TypeScript 3.7
First there's the optional element access which acts similarly to optional property accesses, but allows us to access non-identifier properties (e.g. ...
Read more >Using Optional and Nullable Properties in API Requests
Learn how optional and nullable properties can be used flexibly in combinations in each parameter of your API requests made from a SDK....
Read more >Documentation - TypeScript 2.0
Optional parameters and properties automatically have undefined added to their types, even when their type annotations don't specifically include undefined ...
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
Thank you for great suggestion!
If anyone has problems with the current workaround not functioning correctly for schemas involving inheritance, this seems to be fixed by looping over
Schema.ActualProperties
instead ofSchema.Properties
@Liwoj Our schema processor looks like this:
And registering it looks like this:
I agree that the default behaviour seems strange here.