Created
January 24, 2024 12:59
-
-
Save dariocravero/812257af5db64c0e2d0f23b83e7a7ad2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** @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 }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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