loopbackio / loopbackio/loopback-next
Invalid TS models when properties are nullable
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 5.1k
- Forks
- 1.1k
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 27
Description
Hi! It seems the openapi connector is not setting the correct TS type when setting true the nullable flag
For example, with the following JSON OpenAPI definition model:
"TestObject": {
"type": "object",
"properties": {
"name": {
"type": "string",
"nullable": true,
"default": null,
"description": "Test property",
"example": null
},
It creates a model with the following property:
@property({jsonSchema: {
type: [
'string',
'null',
],
default: null,
description: 'Specific endpoint for this queue, if not specified, the generic endpoint for this service will be used',
}})
endpoint?: string = null;
Obviously, as endpoint is set as string, it throws a TS Check error in build time:
error TS2322: Type 'null' is not assignable to type 'string | undefined'.
Why isn't setting the property type correctly, whereas in the type annotation seems to be aware of it?
Thanks!
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing the OpenAPI connector's TypeScript model generation for a property with nullable: true and default: null. Compare the generated property annotation with its declared type, then run the relevant model-generation checks or TypeScript build; done means nullable properties accept null without TS2322.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- openapi, typescript
- Domain
- api
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100