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

$ref on definition and additionalProperties: false together results to never type #188

Open
tothdanielax opened this issue Feb 8, 2024 · 1 comment

Comments

@tothdanielax
Copy link

tothdanielax commented Feb 8, 2024

I try to refer to a definition and after that make additionalProperties: false. The expected result would be a TS type without [x: string]: unknown;

Simplified example:

export const ciSchemaa = {
    type: "object",
    $defs: {
        ciBaseMetadata: {
            type: "object",
            properties: {
                ciId: {
                    type: "string",
                },
            },
            required: [
                "ciId",
            ]
        },
    },
    $ref: "#/$defs/ciBaseMetadata",
    additionalProperties: false
} as const satisfies JSONSchema;

export type Ci = FromSchema<typeof ciSchema> // never

It is possible to make it work if I already set additionalProperties:false in the definition, but curious if that could be done as the last step.

@mrl5
Copy link

mrl5 commented May 22, 2024

I think it's just a matter of unhydrated $ref. It's mentioned in docs https://github.com/ThomasAribart/json-schema-to-ts?tab=readme-ov-file#references

tested with typescript =5.4.5 and json-schema-to-ts =3.1.0

At least in my example:

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

export const healthCode = {
    $id: 'HealthCode',
    type: 'string',
    enum: ['healthy', 'degraded'],
} as const satisfies JSONSchema;

export const healthRes = {
    type: 'object',
    properties: {
        code: {
            $ref: 'HealthCode#',
        },
    },
    required: ['code'],
    additionalProperties: false,
} as const satisfies JSONSchema;

export type HealthRes = FromSchema<typeof healthRes;

const h: HealthRes = {}; // Type '{}' is not assignable to type 'never'.

all I need to do to make it working is to hydrate the ref like this:

- export type HealthRes = FromSchema<typeof healthRes;
+ export type HealthRes = FromSchema<typeof healthRes, { references: [typeof healthCode] }>;

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

2 participants