|
| 1 | +export type StdPrimitiveMap = { |
| 2 | + string: string |
| 3 | + number: number |
| 4 | + boolean: boolean |
| 5 | +} |
| 6 | +export type StdPrimitives = keyof StdPrimitiveMap |
| 7 | + |
| 8 | +export type TypeDef<P extends string, U extends string = never> = |
| 9 | + | { type: 'struct'; def: Record<string, TypeDef<P, U>> } |
| 10 | + | { type: 'record'; def: TypeDef<P, U> } |
| 11 | + | { type: 'array'; def: TypeDef<P, U> } |
| 12 | + | { type: 'primitive'; def: P | StdPrimitives } |
| 13 | + | { type: 'optional'; def: TypeDef<P, U> } |
| 14 | + | (U extends string ? { type: 'reference'; def: U } : never) |
| 15 | + |
| 16 | +export function Struct<U extends Record<string, TypeDef<any, any>>>(def: U) { |
| 17 | + return { type: 'struct' as const, def: def as U } |
| 18 | +} |
| 19 | + |
| 20 | +export function Record<T extends TypeDef<any, any>>(def: T) { |
| 21 | + return { type: 'record' as const, def: def as T } |
| 22 | +} |
| 23 | + |
| 24 | +export function Arr<T extends TypeDef<any, any>>(def: T) { |
| 25 | + return { type: 'array' as const, def: def as T } |
| 26 | +} |
| 27 | + |
| 28 | +export function Primitive<P extends string>(def: P) { |
| 29 | + return { type: 'primitive', def } as { type: 'primitive'; def: P } |
| 30 | +} |
| 31 | + |
| 32 | +export function Str() { |
| 33 | + return { type: 'primitive' as const, def: 'string' as const } |
| 34 | +} |
| 35 | + |
| 36 | +export function Num() { |
| 37 | + return { type: 'primitive' as const, def: 'number' as const } |
| 38 | +} |
| 39 | + |
| 40 | +export function Bool() { |
| 41 | + return { type: 'primitive' as const, def: 'boolean' as const } |
| 42 | +} |
| 43 | + |
| 44 | +export function Optional<T extends TypeDef<any, any>>(def: T) { |
| 45 | + return { type: 'optional' as const, def } |
| 46 | +} |
| 47 | + |
| 48 | +export function Reference<U extends string>(def: U) { |
| 49 | + return { type: 'reference' as const, def: def as U } |
| 50 | +} |
0 commit comments