json-schema-to-ts icon indicating copy to clipboard operation
json-schema-to-ts copied to clipboard

Properties with const are not typed as const.

Open floratmin opened this issue 8 months ago • 1 comments

When a schema has a property with const value, then the inferred type should also be const. E.g.:

const schema = {
  $id: 'myschema',
  title: 'MySchema',
  type: 'object',
  required: ['myprop'],
  additionalProperties: false,
  properties: {
    myprop: {
      const: 500,
    },
  },
} as const;

type Schema = FromSchema<typeof schema>;

This works if I use Schema directly. But if I make some type transformations this gets lost and the type of myprop becomes number, because there is no readonly annotation for this property.

schema: {
  readonly $id: "myschema", 
  readonly title: "MySchema",
  readonly type: "object",
  readonly required: readonly ["myprop"],
  readonly properties: {
    myprop: 500
  },
  readonly additionalProperties: false
};

floratmin avatar Nov 09 '23 23:11 floratmin