From 4ebfda9899aff9105acc99c68074acd32a7d3680 Mon Sep 17 00:00:00 2001 From: Dirk Holtwick Date: Wed, 28 Aug 2024 17:47:56 +0200 Subject: [PATCH 1/2] fix: tree shake --- src/common/schema/args.spec.ts | 4 ++++ src/common/schema/schema.spec.ts | 1 + src/common/schema/schema.ts | 35 ++++++++++++++++++-------------- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/common/schema/args.spec.ts b/src/common/schema/args.spec.ts index c2b3227..be51d8b 100644 --- a/src/common/schema/args.spec.ts +++ b/src/common/schema/args.spec.ts @@ -1,4 +1,5 @@ import { helpSchemaArgs, parseSchemaArgs } from './args' +import type { Infer } from './schema' import { boolean, number, object } from './schema' describe('args.spec', () => { @@ -13,6 +14,9 @@ describe('args.spec', () => { }), }) + type t = Infer + expectTypeOf().toMatchTypeOf() + const args = ['test.txt', '-h', '--some-stuff=8888'] const r = parseSchemaArgs(schema, args) diff --git a/src/common/schema/schema.spec.ts b/src/common/schema/schema.spec.ts index 4b3751c..1326e66 100644 --- a/src/common/schema/schema.spec.ts +++ b/src/common/schema/schema.spec.ts @@ -68,6 +68,7 @@ describe('schema', () => { "type": "int", }, "id": Object { + "_default": "123", "type": "string", }, "lit": Object { diff --git a/src/common/schema/schema.ts b/src/common/schema/schema.ts index f94c62e..8409d3f 100644 --- a/src/common/schema/schema.ts +++ b/src/common/schema/schema.ts @@ -8,7 +8,11 @@ export interface TypeProps { // message?: string // } -interface Type { +export interface Type { + _default?: T + _optional?: boolean + _props?: TypeProps + _object?: T readonly type: string readonly _check: (obj: any) => boolean optional: () => Type @@ -18,7 +22,7 @@ interface Type { props: (props: TypeProps) => Type } -class TypeClass implements Type { +export abstract class TypeClass implements Type { readonly type readonly _check @@ -27,6 +31,8 @@ class TypeClass implements Type { this._check = check ?? (() => true) } + _object?: T + _optional?: boolean optional(): TypeClass { // todo keep the inherited class type @@ -75,8 +81,11 @@ export type Infer = T extends Type ? TT : never // Helper +class TypeGeneric extends TypeClass { +} + function generic(type: string, opt?: Partial>): Type { - return new TypeClass(type, opt?._check) + return new TypeGeneric(type, opt?._check) } // Primitives @@ -128,16 +137,18 @@ type ObjectFixOptional = { type ObjectPretty = Extract<{ [K in keyof V]: V[K] }, unknown> -export type TypeObject = Type = ObjectPretty -}>>> +}>> -class TypeObjectClass> extends TypeClass { - _object: T +// interface TypeObject extends Type { +// _object: T +// } +export class TypeObjectClass> extends TypeClass { constructor(obj: T) { super('object', () => true) - this._object = obj + this._object = obj as any } parse(obj: any) { @@ -172,16 +183,10 @@ class TypeObjectClass> extends TypeClass { } } -export function object(tobj: T): TypeObject { +export function object(tobj: T): Type> { return new TypeObjectClass(tobj) as any } -const x = object({ - name: string(), -}) - -type xt = Infer - // Union type TransformToUnion)[]> = T extends Array ? Infer : never From 2633605be8b5af98ee4653c1eb21e04371d2c742 Mon Sep 17 00:00:00 2001 From: Dirk Holtwick Date: Wed, 28 Aug 2024 17:47:58 +0200 Subject: [PATCH 2/2] 0.24.11 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ed76ab7..19f0435 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "zeed", "type": "module", - "version": "0.24.10", + "version": "0.24.11", "description": "🌱 Simple foundation library", "author": { "name": "Dirk Holtwick",