I have a TypeScript file containing an openapi object schema constant:
export default {
"title": "Draft",
"description": "A new draft listing",
"type": "object",
"additionalProperties": false,
"required": [
"id"
],
"properties": {
"id": {
"type": "string"
}
}
}
Now, I'm attempting to include this schema as a component in the general OpenAPI schema in another file:
import Draft from './__generated_schemas__/draft.js'
import { OpenAPIV3 } from 'openapi-types'
export const schema: OpenAPIV3.Document = {
openapi: '3.1',
info: {
title: 'Properties API',
version: '1.0.0',
description:
'Nice service description'
},
components: {
schemas: {
Draft
}
},
paths: {}
}
However, TypeScript throws an error when I do this:
packages/api/src/schema.ts(22,7): error TS2322: Type '{ title: string; description: string; type: string; additionalProperties: boolean; required: string[]; properties: { id: { type: string; }; }; }' is not assignable to type 'ReferenceObject | SchemaObject'.
Type '{ title: string; description: string; type: string; additionalProperties: boolean; required: string[]; properties: { id: { type: string; }; }; }' is not assignable to type 'NonArraySchemaObject'.
Types of property 'type' are incompatible.
Type 'string' is not assignable to type 'NonArraySchemaObjectType | undefined'.
Interestingly, if I manually insert the JSON from the TypeScript file into the OpenAPI schema, everything works fine:
export const schema: OpenAPIV3.Document = {
openapi: '3.1',
info: {
title: 'Properties API',
version: '1.0.0',
description:
'Nice service description'
},
components: {
schemas: {
Draft: {
"title": "Draft",
"description": "A new draft listing",
"type": "object",
"additionalProperties": false,
"required": [
"id"
],
"properties": {
"id": {
"type": "string"
}
}
}
}
}
Any insight on why TypeScript type checking fails in the former case? The object schema and overall OpenAPI schema seem correct to me...