Skip to content

Commit

Permalink
Run some experiments, clean configs (#3650)
Browse files Browse the repository at this point in the history
* 3.23.5

* Experiments

* WIP

* WIP

* Ignore tshy

* Fix type errors

* Fix tsconfigs
  • Loading branch information
colinhacks authored Jul 18, 2024
1 parent 957da06 commit b3b36b7
Show file tree
Hide file tree
Showing 12 changed files with 328 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .configs/tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"strictPropertyInitialization": false,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"noUnusedLocals": true,
"noUnusedLocals": false,
"noUnusedParameters": true,
"noImplicitReturns": true,

Expand Down
8 changes: 0 additions & 8 deletions .configs/tsconfig.test.json

This file was deleted.

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ out.png
logos
.zed
dist
.tshy
.tshy-build
2 changes: 1 addition & 1 deletion benchmarks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"private": true,
"name": "zod-benchmarks",
"devDependencies": {
"zod": "3.23.5"
"zod": "3.23.8"
}
}
5 changes: 4 additions & 1 deletion benchmarks/tsconfig.json
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
}
}
3 changes: 2 additions & 1 deletion biome.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"rules": {
"recommended": true,
"suspicious": {
"noExplicitAny": "off" // `any` is amazing
"noExplicitAny": "off", // `any` is amazing
"noUnsafeDeclarationMerging": "off"
},
"style": {
"noUnusedTemplateLiteral": "off", // why is this even a best practice?
Expand Down
120 changes: 120 additions & 0 deletions experiments/mixins.ts
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;
};
95 changes: 95 additions & 0 deletions experiments/totems.ts
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 } };
}
>;
92 changes: 92 additions & 0 deletions experiments/totems_v2.ts
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 } };
}
>;
6 changes: 5 additions & 1 deletion packages/effect-plugin/tests/tsconfig.json
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": "."
}
}
3 changes: 1 addition & 2 deletions packages/zod/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zod",
"version": "3.23.6",
"version": "3.23.8",
"author": "Colin McDonnell <zod@colinhacks.com>",
"description": "TypeScript-first schema declaration and validation library with static type inference",
"files": ["src", "dist"],
Expand All @@ -25,7 +25,6 @@
"import": "./dist/esm/locales/*.js"
}
},

"repository": {
"type": "git",
"url": "git+https://github.com/colinhacks/zod.git"
Expand Down
Loading

0 comments on commit b3b36b7

Please sign in to comment.