Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Definitons in references not resolved #172

Open
antonio-ivanovski opened this issue Oct 17, 2023 · 0 comments
Open

Definitons in references not resolved #172

antonio-ivanovski opened this issue Oct 17, 2023 · 0 comments

Comments

@antonio-ivanovski
Copy link

antonio-ivanovski commented Oct 17, 2023

When there is referenced schema that contains properties that contains property defined in the definitions, the property type is being resolved to undefined.

Example:

import type { FromSchema, JSONSchema } from 'json-schema-to-ts';

const FooSchema = {
    $schema: 'http://json-schema.org/draft-07/schema',
    type: 'object',
    properties: {
        bar: {
            $ref: 'https://example.com/bar.json',
        },
    },
} as const satisfies JSONSchema;

const BarSchema = {
    $schema: 'http://json-schema.org/draft-07/schema',
    $id: 'https://example.com/bar.json',
    type: 'object',
    definitions: {
        name: {
            type: 'string',
        },
    },
    properties: {
        name: {
            $ref: '#/definitions/name',
        },
        age: {
            type: 'number',
        },
    },
} as const satisfies JSONSchema;

type FooType = FromSchema<typeof FooSchema, { references: [typeof BarSchema] }>;
// Result: type FooType = {
//   [x: string]: unknown;
//   bar?: {
//       [x: string]: unknown;
//       name?: undefined;
//       age?: number | undefined;
//   } | undefined;
// }

const foo: FooType = {
    bar: {
        name: 'Antonio', // Error: Type 'string' is not assignable to type 'undefined'
        age: 42,
    },
};

In the README there is the line under the Definitions section that states ☝️ Wether in definitions or references, FromSchema will not work on recursive schemas for now. (link)

If this is not supposed to be supported, is there another workaround that we can get the type even in some more manual way?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant