json-schema-to-ts
json-schema-to-ts copied to clipboard
Properties with const are not typed as const.
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
};