Adds a .toType
method to all schemas that prints a stringified version of the type that the schema represents.
Works at both the term- and type-level.
Works on both the term- and type-level.
Instructions: To install the .toType
method on all schemas, simply import @traversable/schema-to-string/install
.
Caveat: type-level functionality is provided as a heuristic only; since object keys are unordered in the TS type system, the order that the keys are printed at runtime might differ from the order they appear on the type-level.
Play with this example in the TypeScript playground
import { t } from '@traversable/schema'
import '@traversable/schema-to-string/install'
// ↑↑ importing `@traversable/schema-to-string/install` adds the upgraded `.toType` method on all schemas
const schema_02 = t.intersect(
t.object({
bool: t.optional(t.boolean),
nested: t.object({
int: t.integer,
union: t.union(t.tuple(t.string), t.null),
}),
key: t.union(t.string, t.symbol, t.number),
}),
t.object({
record: t.record(t.string),
maybeArray: t.optional(t.array(t.string)),
enum: t.enum('x', 'y', 1, 2, null),
}),
)
let ex_02 = schema_02.toType()
// ^? let ex_02: "({
// 'bool'?: (boolean | undefined),
// 'nested': { 'int': number, 'union': ([string] | null) },
// 'key': (string | symbol | number) }
// & {
// 'record': Record<string, string>,
// 'maybeArray'?: ((string)[] | undefined),
// 'enum': 'x' | 'y' | 1 | 2 | null
// })"