Skip to content

Instantly share code, notes, and snippets.

@dariocravero
Created January 24, 2024 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dariocravero/812257af5db64c0e2d0f23b83e7a7ad2 to your computer and use it in GitHub Desktop.
Save dariocravero/812257af5db64c0e2d0f23b83e7a7ad2 to your computer and use it in GitHub Desktop.
/** @satisfies {import('json-schema-to-ts').JSONSchema} */
const schema2 = {
type: 'object',
additionalProperties: false,
properties: {
input: {
type: 'object',
additionalProperties: false,
properties: {
x: { type: 'number' },
},
},
},
}
/** @type {import('json-schema-to-ts').FromSchema<typeof schema2>} */
const value2 = {
input: {
x: 1,
},
}
// const value2: {
// input?: {
// x?: number | undefined;
// } | undefined;
// }
// @type — {import('json-schema-to-ts').FromSchema} Type
/** @satisfies {import('json-schema-to-ts').JSONSchema} */
const schemaRequired2 = {
type: 'object',
required: ['input'],
additionalProperties: false,
properties: {
input: {
type: 'object',
additionalProperties: false,
required: ['x'],
properties: {
x: { type: 'number' },
},
},
},
}
/** @type {import('json-schema-to-ts').FromSchema<typeof schemaRequired2>} */
const valueRequired2 = {
input: {
x: 2,
},
}
// Type '{ input: { x: number; }; }' is not assignable to type 'never'.ts(2322)
// const valueRequired2: never
// @type — {import('json-schema-to-ts').FromSchema}
console.log({ schema2, value2, schemaRequired2, valueRequired2 })
import { FromSchema, JSONSchema } from 'json-schema-to-ts'
const schema = {
type: 'object',
additionalProperties: false,
properties: {
input: {
type: 'object',
additionalProperties: false,
properties: {
x: { type: 'number' },
},
},
},
} as const satisfies JSONSchema
type Schema = FromSchema<typeof schema>
// type Schema = {
// input?: {
// x?: number | undefined;
// } | undefined;
// }
const value: Schema = {
input: {
x: 1,
},
}
// const value: {
// input?: {
// x?: number | undefined;
// } | undefined;
// }
const schemaRequired = {
type: 'object',
required: ['input'],
additionalProperties: false,
properties: {
input: {
type: 'object',
additionalProperties: false,
required: ['x'],
properties: {
x: { type: 'number' },
},
},
},
} as const satisfies JSONSchema
type SchemaRequired = FromSchema<typeof schemaRequired>
// type SchemaRequired = {
// input: {
// x: number;
// };
// }
const valueRequired: SchemaRequired = {
input: {
x: 1,
},
}
// const valueRequired: {
// input: {
// x: number;
// };
// }
console.log({ schema, value, schemaRequired, valueRequired })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment