Skip to content

Commit

Permalink
Merge branch 'release/v0.24.11'
Browse files Browse the repository at this point in the history
  • Loading branch information
holtwick committed Aug 28, 2024
2 parents cac3b27 + 2633605 commit 693de2a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "zeed",
"type": "module",
"version": "0.24.10",
"version": "0.24.11",
"description": "🌱 Simple foundation library",
"author": {
"name": "Dirk Holtwick",
Expand Down
4 changes: 4 additions & 0 deletions src/common/schema/args.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { helpSchemaArgs, parseSchemaArgs } from './args'
import type { Infer } from './schema'
import { boolean, number, object } from './schema'

describe('args.spec', () => {
Expand All @@ -13,6 +14,9 @@ describe('args.spec', () => {
}),
})

type t = Infer<typeof schema>
expectTypeOf<t>().toMatchTypeOf<any>()

const args = ['test.txt', '-h', '--some-stuff=8888']

const r = parseSchemaArgs(schema, args)
Expand Down
1 change: 1 addition & 0 deletions src/common/schema/schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ describe('schema', () => {
"type": "int",
},
"id": Object {
"_default": "123",
"type": "string",
},
"lit": Object {
Expand Down
35 changes: 20 additions & 15 deletions src/common/schema/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ export interface TypeProps {
// message?: string
// }

interface Type<T = unknown> {
export interface Type<T = unknown> {
_default?: T
_optional?: boolean
_props?: TypeProps
_object?: T
readonly type: string
readonly _check: (obj: any) => boolean
optional: () => Type<T | undefined>
Expand All @@ -18,7 +22,7 @@ interface Type<T = unknown> {
props: (props: TypeProps) => Type<T>
}

class TypeClass<T = unknown> implements Type<T> {
export abstract class TypeClass<T = unknown> implements Type<T> {
readonly type
readonly _check

Expand All @@ -27,6 +31,8 @@ class TypeClass<T = unknown> implements Type<T> {
this._check = check ?? (() => true)
}

_object?: T

_optional?: boolean

optional(): TypeClass<T | undefined> { // todo keep the inherited class type
Expand Down Expand Up @@ -75,8 +81,11 @@ export type Infer<T> = T extends Type<infer TT> ? TT : never

// Helper

class TypeGeneric<T> extends TypeClass<T> {
}

function generic<T>(type: string, opt?: Partial<Type<T>>): Type<T> {
return new TypeClass<T>(type, opt?._check)
return new TypeGeneric<T>(type, opt?._check)
}

// Primitives
Expand Down Expand Up @@ -128,16 +137,18 @@ type ObjectFixOptional<T> = {

type ObjectPretty<V> = Extract<{ [K in keyof V]: V[K] }, unknown>

export type TypeObject<T = unknown> = Type<ObjectPretty<ObjectFixOptional<{
export type InferObject<T> = ObjectPretty<ObjectFixOptional<{
[K in keyof T]: Infer<T[K]>
}>>>
}>>

class TypeObjectClass<T, O = TypeObject<T>> extends TypeClass<O> {
_object: T
// interface TypeObject<T = unknown> extends Type<T> {
// _object: T
// }

export class TypeObjectClass<T, O = InferObject<T>> extends TypeClass<O> {
constructor(obj: T) {
super('object', () => true)
this._object = obj
this._object = obj as any
}

parse(obj: any) {
Expand Down Expand Up @@ -172,16 +183,10 @@ class TypeObjectClass<T, O = TypeObject<T>> extends TypeClass<O> {
}
}

export function object<T>(tobj: T): TypeObject<T> {
export function object<T>(tobj: T): Type<InferObject<T>> {
return new TypeObjectClass(tobj) as any
}

const x = object({
name: string(),
})

type xt = Infer<typeof x>

// Union

type TransformToUnion<T extends (Type<any>)[]> = T extends Array<infer U> ? Infer<U> : never
Expand Down

0 comments on commit 693de2a

Please sign in to comment.