-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Run some experiments, clean configs (#3650)
* 3.23.5 * Experiments * WIP * WIP * Ignore tshy * Fix type errors * Fix tsconfigs
- Loading branch information
1 parent
957da06
commit b3b36b7
Showing
12 changed files
with
328 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,5 @@ out.png | |
logos | ||
.zed | ||
dist | ||
.tshy | ||
.tshy-build |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,6 @@ | |
"private": true, | ||
"name": "zod-benchmarks", | ||
"devDependencies": { | ||
"zod": "3.23.5" | ||
"zod": "3.23.8" | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
{ | ||
"extends": "../.configs/tsconfig.test.json" | ||
"extends": "../.configs/tsconfig.base.json", | ||
"compilerOptions": { | ||
"isolatedDeclarations": false | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
type HasInstance = (instance: any) => boolean; | ||
function checkSymbol(sym: string): (instance: any) => boolean { | ||
return (instance: any) => instance[sym] === true; | ||
} | ||
function attachTag(c: Constructor, sym: string): void { | ||
c[Symbol.hasInstance] = checkSymbol(sym); | ||
(c.prototype as any)[sym] = true; | ||
} | ||
|
||
// const $ZOD_TYPE: unique symbol = Symbol.for("{{$ZOD_TYPE}}"); | ||
// type $ZOD_TYPE = typeof $ZOD_TYPE; | ||
export abstract class $ZodType<T = unknown> { | ||
_type: T; | ||
"{{zod_type_v4}}": true; | ||
|
||
abstract parse(data: unknown): this["_type"]; | ||
} | ||
attachTag($ZodType, "{{zod_type_v4}}"); | ||
|
||
export class $ZodString extends $ZodType<string> { | ||
"{{ZOD_STRING}}": boolean; | ||
parse(_data: unknown): this["_type"] { | ||
return "asdf"; | ||
} | ||
} | ||
attachTag($ZodString, "$ZOD_STRING"); | ||
|
||
const ZOD_OPTIONAL: unique symbol = Symbol.for("{{ZOD_OPTIONAL}}"); | ||
type ZOD_OPTIONAL = typeof ZOD_OPTIONAL; | ||
export interface $ZodOptional<T extends $ZodType> { | ||
[ZOD_OPTIONAL]: boolean; | ||
} | ||
export class $ZodOptional<T extends $ZodType> extends $ZodType< | ||
T["_type"] | undefined | ||
> { | ||
"~~~": this["~~~"] & { | ||
[ZOD_OPTIONAL]: true; | ||
}; | ||
// static [Symbol.hasInstance: unique symbol]: HasInstance = checkSymbol(ZOD_OPTIONAL) as HasInstance; | ||
constructor(public inner: T) { | ||
super(); | ||
} | ||
// protected [ZOD_OPTIONAL]: boolean; | ||
parse(_data: unknown): T["_type"] | undefined { | ||
return "asdf"; | ||
} | ||
} | ||
attachTag($ZodOptional, "ZOD_OPTIONAL"); | ||
|
||
// declare let x: $ZodOptional<$ZodString>; | ||
// x[$ZOD_TYPE]; | ||
|
||
// type Constructor<T extends $ZodType> = new (...args: any[]) => T; | ||
type Constructor = abstract new (...args: any[]) => $ZodType; | ||
// const ZOD_TYPE = Symbol.for("{{ZOD_OPTIONAL}}"); | ||
|
||
const ZodStringMixin: ZodTypeMixed<typeof $ZodType> = ZodTypeMixin($ZodType); | ||
export abstract class ZodType extends ZodStringMixin { | ||
constructor(..._args: any[]) { | ||
super(); | ||
} | ||
} | ||
type ZodTypeMixed<T extends Constructor> = { | ||
new (...args: ConstructorParameters<T>): InstanceType<T> & ZodTypeMixin; | ||
}; | ||
export function ZodTypeMixin<T extends Constructor>( | ||
SuperClass: T | ||
): ZodTypeMixed<T> { | ||
abstract class $Temp extends SuperClass { | ||
protected "{{ZOD_OPTIONAL}}": true; | ||
static [Symbol.hasInstance]: HasInstance = checkSymbol("{{ZOD_OPTIONAL}}"); | ||
// constructor(...args: any[]) { | ||
// super(...args); | ||
// } | ||
optional() { | ||
return new $ZodOptional(this); | ||
} | ||
|
||
constant<T extends string>(_val: T): $constant<this, T> { | ||
return this as any; | ||
} | ||
// parse(data: unknown): this["_type"] { | ||
// throw new Error("Method not implemented."); | ||
// } | ||
} | ||
return $Temp as any; | ||
} | ||
|
||
interface ZodTypeMixin extends $ZodType { | ||
// optional(): ZodOptional<this>; | ||
constant<T extends string>(val: T): $constant<this, T>; | ||
} | ||
|
||
const ZOD_STRING: unique symbol = Symbol.for("{{ZOD_STRING}}"); | ||
type ZOD_STRING = typeof ZOD_STRING; | ||
export const ZodStringBase: ZodTypeMixed<typeof $ZodString> = | ||
ZodTypeMixin($ZodString); | ||
export class ZodString extends ZodStringBase { | ||
"~def": { | ||
[ZOD_STRING]: true; | ||
}; | ||
} | ||
attachTag(ZodString, "$ZOD_STRING"); | ||
|
||
declare const t: ZodString; | ||
|
||
// const asdf = ZodTypeMixin($ZodString); | ||
|
||
const s0 = new $ZodString(); | ||
const v0 = s0.parse("asdf"); | ||
const s1 = new ZodString(); | ||
const v1 = s1.parse("asdf"); | ||
const s2 = s1.constant("sup"); | ||
s2._type; | ||
const v2 = s2.parse("asdf"); | ||
|
||
s2.parse("asdf"); | ||
type $constant<T extends $ZodType, Value extends string> = T & { | ||
_type: Value; | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
interface ZodT { | ||
"~input": unknown; | ||
"~output": unknown; | ||
"~async": boolean; | ||
"~meta": unknown; | ||
} | ||
|
||
type Prettify<T> = { | ||
[K in keyof T]: T[K]; | ||
} & {}; | ||
|
||
// type $assign<Old extends ZodT, New extends ZodT> = Prettify< | ||
// Omit<Old, keyof New> & New | ||
// >; | ||
type $merge<Old extends ZodT, New extends Partial<ZodT>> = Prettify<{ | ||
"~overrides": true; | ||
"~input": "~input" extends keyof New ? New["~input"] : Old["~input"]; | ||
"~output": "~output" extends keyof New ? New["~output"] : Old["~output"]; | ||
"~async": "~async" extends keyof New ? New["~async"] : Old["~async"]; | ||
"~meta": "~meta" extends keyof New | ||
? Old["~meta"] & New["~meta"] | ||
: Old["~meta"]; | ||
}>; | ||
// type $override<T extends $ZodType<any>, O extends Partial<ZodT>> = { | ||
// "~override": true; | ||
// "~base": "~base" extends keyof T ? T["~base"] : T; | ||
// "~input": "~input" extends keyof O ? O["~input"] : T["~input"]; | ||
// "~output": "~output" extends keyof O ? O["~output"] : T["~output"]; | ||
// "~async": "~async" extends keyof O ? O["~async"] : T["~async"]; | ||
// "~meta": "~meta" extends keyof O ? O["~meta"] : T["~meta"]; | ||
// } & T["~base"]; | ||
type $override<T extends $ZodType, O extends ZodT> = T & { "~overrides": O }; | ||
type $meta<M> = { "~overrides": { "~meta": M } }; | ||
abstract class $ZodType { | ||
"~base": $ZodType; | ||
"~overrides": ZodT; | ||
// "~input": T["~input"]; | ||
// "~output": T["~output"]; | ||
// "~async": T["~async"]; | ||
// "~meta": T["~meta"]; | ||
"~output": this["~overrides"]["~async"] extends true | ||
? Promise<this["~overrides"]["~output"]> | ||
: this["~overrides"]["~output"]; | ||
|
||
abstract parse(data: unknown): this["~output"]; | ||
|
||
meta<T>(_data: T): this & $meta<T> { | ||
// $override<this["~base"], $merge<this["~overrides"], { "~meta": T }>> { | ||
return this as any; | ||
} | ||
|
||
async(): $override< | ||
this["~base"], | ||
$merge<this["~overrides"], { "~async": true }> | ||
> { | ||
return this as any; | ||
} | ||
} | ||
|
||
class $ZodString extends $ZodType { | ||
override "~base": $ZodString; | ||
|
||
parse(data: unknown): this["~output"] { | ||
return data as string; | ||
} | ||
} | ||
|
||
interface ZodString extends $ZodType { | ||
"~overrides": { | ||
"~input": string; | ||
"~output": string; | ||
"~async": false; | ||
"~meta": unknown; | ||
}; | ||
} | ||
|
||
function string(): ZodString { | ||
return new $ZodString() as any; | ||
} | ||
|
||
const s1 = string(); | ||
s1["~overrides"]; | ||
s1.parse("hello"); | ||
const s2 = s1.meta({ hello: "there" }); //.async(); | ||
const _d2 = s2.parse("hello"); | ||
|
||
export type {}; | ||
|
||
type alksjdf = Prettify< | ||
{ | ||
overrides: { meta: { name: "Colin" } }; | ||
} & { | ||
overrides: { meta: { age: 30 } }; | ||
} | ||
>; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
interface ZodT { | ||
"~input"?: unknown; | ||
"~output"?: unknown; | ||
"~async"?: true; | ||
"~meta"?: unknown; | ||
"~optional"?: true; | ||
} | ||
|
||
type isOptional<T extends ZodT> = T["~optional"] extends true ? true : false; | ||
type asdf = isOptional<ZodT>; | ||
|
||
type Prettify<T> = { | ||
[K in keyof T]: T[K]; | ||
} & {}; | ||
|
||
type $override<T extends $ZodType, O extends ZodT> = T & { "~overrides": O }; | ||
type $meta<M> = { "~overrides": { "~meta": M } }; | ||
type $async = { "~overrides": { "~async": true } }; | ||
type $output<T, O> = T & { "~overrides": { "~output": O } }; | ||
type $input<T, I> = T & { "~overrides": { "~input": I } }; | ||
type $optional = { "~overrides": { "~optional": true } }; | ||
abstract class $ZodType { | ||
"~overrides": ZodT; | ||
"~output": this["~overrides"]["~async"] extends true | ||
? Promise<this["~overrides"]["~output"]> | ||
: this["~overrides"]["~output"]; | ||
|
||
abstract parse(data: unknown): this["~output"]; | ||
|
||
meta<T>(_data: T): this & $meta<T> { | ||
return this as any; | ||
} | ||
|
||
async(): this & $async { | ||
return this as any; | ||
} | ||
|
||
optional(): this & $optional { | ||
return { | ||
...this, | ||
"~overrides": { | ||
...this["~overrides"], | ||
"~optional": true, | ||
}, | ||
}; | ||
} | ||
} | ||
|
||
interface Methods { | ||
method(): boolean; | ||
} | ||
interface $ZodString extends Methods {} | ||
// class $ZodString extends $ZodType { | ||
// override | ||
// parse(data: unknown): this["~output"] { | ||
// return data as string; | ||
// } | ||
// } | ||
|
||
class $ZodString extends $ZodType { | ||
override "~overrides": { | ||
"~input": string; | ||
"~output": string; | ||
}; | ||
parse(data: unknown): this["~output"] { | ||
return data as this["~output"]; | ||
} | ||
} | ||
|
||
class ZodString extends $ZodString {} | ||
|
||
function string(): ZodString { | ||
return new $ZodString() as any; | ||
} | ||
|
||
const s1 = string(); | ||
s1["~overrides"]; | ||
const d1 = s1.parse("hello"); | ||
const s2 = s1.meta({ hello: "there" }); | ||
const d2 = s2.parse("hello"); | ||
const s3 = s2.async(); | ||
const d3 = s3.parse("hello"); | ||
|
||
export type {}; | ||
|
||
type alksjdf = Prettify< | ||
{ | ||
overrides: { meta: { name: "Colin" } }; | ||
} & { | ||
overrides: { meta: { age: 30 } }; | ||
} | ||
>; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
{ | ||
"extends": "../../../.configs/tsconfig.test.json" | ||
"extends": "../../../.configs/tsconfig.base.json", | ||
"compilerOptions": { | ||
"isolatedDeclarations": false, | ||
"rootDir": "." | ||
} | ||
} |
This file contains 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
Oops, something went wrong.