From ef3f1aedb6e09fa15bcfb9b16da05fc45b1a0038 Mon Sep 17 00:00:00 2001 From: Netanel Gilad Date: Fri, 18 Oct 2024 12:30:13 +0000 Subject: [PATCH 1/2] fix record type with branded keys --- deno/lib/README.md | 4 ++++ deno/lib/__tests__/record.test.ts | 24 ++++++++++++++++++++++++ deno/lib/types.ts | 9 ++++++++- src/__tests__/record.test.ts | 24 ++++++++++++++++++++++++ src/types.ts | 9 ++++++++- 5 files changed, 68 insertions(+), 2 deletions(-) diff --git a/deno/lib/README.md b/deno/lib/README.md index a721693b1..cdae9403c 100644 --- a/deno/lib/README.md +++ b/deno/lib/README.md @@ -491,6 +491,7 @@ There are a growing number of tools that are built atop or support Zod natively! - [`sveltekit-superforms`](https://github.com/ciscoheat/sveltekit-superforms): Supercharged form library for SvelteKit with Zod validation. - [`mobx-zod-form`](https://github.com/MonoidDev/mobx-zod-form): Data-first form builder based on MobX & Zod. - [`@vee-validate/zod`](https://github.com/logaretm/vee-validate/tree/main/packages/zod): Form library for Vue.js with Zod schema validation. +- [`zod-form-renderer`](https://github.com/thepeaklab/zod-form-renderer): Auto-infer form fields from zod schema and render them with react-hook-form with E2E type safety. #### Zod to X @@ -505,6 +506,7 @@ There are a growing number of tools that are built atop or support Zod natively! - [`zod-openapi`](https://github.com/samchungy/zod-openapi): Create full OpenAPI v3.x documentation from Zod schemas. - [`fastify-zod-openapi`](https://github.com/samchungy/fastify-zod-openapi): Fastify type provider, validation, serialization and @fastify/swagger support for Zod schemas. - [`typeschema`](https://typeschema.com/): Universal adapter for schema validation. +- [`zodex`](https://github.com/commonbaseapp/zodex): (De)serialization for zod schemas #### X to Zod @@ -538,11 +540,13 @@ There are a growing number of tools that are built atop or support Zod natively! - [`freerstore`](https://github.com/JacobWeisenburger/freerstore): Firestore cost optimizer. - [`slonik`](https://github.com/gajus/slonik/tree/gajus/add-zod-validation-backwards-compatible#runtime-validation-and-static-type-inference): Node.js Postgres client with strong Zod integration. +- [`schemql`](https://github.com/a2lix/schemql): Enhances your SQL workflow by combining raw SQL with targeted type safety and schema validation. - [`soly`](https://github.com/mdbetancourt/soly): Create CLI applications with zod. - [`pastel`](https://github.com/vadimdemedes/pastel): Create CLI applications with react, zod, and ink. - [`zod-xlsx`](https://github.com/sidwebworks/zod-xlsx): A xlsx based resource validator using Zod schemas. - [`znv`](https://github.com/lostfictions/znv): Type-safe environment parsing and validation for Node.js with Zod schemas. - [`zod-config`](https://github.com/alexmarqs/zod-config): Load configurations across multiple sources with flexible adapters, ensuring type safety with Zod. +- [`unplugin-environment`](https://github.com/r17x/js/tree/main/packages/unplugin-environment#readme): A plugin for loading enviroment variables safely with schema validation, simple with virtual module, type-safe with intellisense, and better DX 🔥 🚀 👷. Powered by Zod. #### Utilities for Zod diff --git a/deno/lib/__tests__/record.test.ts b/deno/lib/__tests__/record.test.ts index 311f805a7..734687eca 100644 --- a/deno/lib/__tests__/record.test.ts +++ b/deno/lib/__tests__/record.test.ts @@ -17,6 +17,20 @@ const recordWithLiteralKeys = z.record( ); type recordWithLiteralKeys = z.infer; +const StringBrand = z.string().brand("StringBrand"); +type StringBrand = z.infer; +const NumberBrand = z.number().brand("NumberBrand"); +type NumberBrand = z.infer; +const SymbolBrand = z.symbol().brand("SymbolBrand"); +type SymbolBrand = z.infer; + +const recordWithBrandedStringKeys = z.record(StringBrand, z.number()); +type recordWithBrandedStringKeys = z.infer; +const recordWithBrandedNumberKeys = z.record(NumberBrand, z.number()); +type recordWithBrandedNumberKeys = z.infer; +const recordWithBrandedSymbolKeys = z.record(SymbolBrand, z.number()); +type recordWithBrandedSymbolKeys = z.infer; + test("type inference", () => { util.assertEqual>(true); @@ -29,6 +43,16 @@ test("type inference", () => { recordWithLiteralKeys, Partial> >(true); + + util.assertEqual>( + true + ); + util.assertEqual>( + true + ); + util.assertEqual>( + true + ); }); test("methods", () => { diff --git a/deno/lib/types.ts b/deno/lib/types.ts index bb2f08519..d0fc3a103 100644 --- a/deno/lib/types.ts +++ b/deno/lib/types.ts @@ -3531,7 +3531,11 @@ export type RecordType = [ ? Record : [symbol] extends [K] ? Record - : [BRAND] extends [K] + : [string & AnyBrand] extends [K] + ? Record + : [number & AnyBrand] extends [K] + ? Record + : [symbol & AnyBrand] extends [K] ? Record : Partial>; export class ZodRecord< @@ -4904,6 +4908,9 @@ export const BRAND: unique symbol = Symbol("zod_brand"); export type BRAND = { [BRAND]: { [k in T]: true }; }; +export type AnyBrand = { + [BRAND]: any; +}; export class ZodBranded< T extends ZodTypeAny, diff --git a/src/__tests__/record.test.ts b/src/__tests__/record.test.ts index 86f60b267..14254bbef 100644 --- a/src/__tests__/record.test.ts +++ b/src/__tests__/record.test.ts @@ -16,6 +16,20 @@ const recordWithLiteralKeys = z.record( ); type recordWithLiteralKeys = z.infer; +const StringBrand = z.string().brand("StringBrand"); +type StringBrand = z.infer; +const NumberBrand = z.number().brand("NumberBrand"); +type NumberBrand = z.infer; +const SymbolBrand = z.symbol().brand("SymbolBrand"); +type SymbolBrand = z.infer; + +const recordWithBrandedStringKeys = z.record(StringBrand, z.number()); +type recordWithBrandedStringKeys = z.infer; +const recordWithBrandedNumberKeys = z.record(NumberBrand, z.number()); +type recordWithBrandedNumberKeys = z.infer; +const recordWithBrandedSymbolKeys = z.record(SymbolBrand, z.number()); +type recordWithBrandedSymbolKeys = z.infer; + test("type inference", () => { util.assertEqual>(true); @@ -28,6 +42,16 @@ test("type inference", () => { recordWithLiteralKeys, Partial> >(true); + + util.assertEqual>( + true + ); + util.assertEqual>( + true + ); + util.assertEqual>( + true + ); }); test("methods", () => { diff --git a/src/types.ts b/src/types.ts index 5aa30b900..999bfae48 100644 --- a/src/types.ts +++ b/src/types.ts @@ -3531,7 +3531,11 @@ export type RecordType = [ ? Record : [symbol] extends [K] ? Record - : [BRAND] extends [K] + : [string & AnyBrand] extends [K] + ? Record + : [number & AnyBrand] extends [K] + ? Record + : [symbol & AnyBrand] extends [K] ? Record : Partial>; export class ZodRecord< @@ -4904,6 +4908,9 @@ export const BRAND: unique symbol = Symbol("zod_brand"); export type BRAND = { [BRAND]: { [k in T]: true }; }; +export type AnyBrand = { + [BRAND]: any; +}; export class ZodBranded< T extends ZodTypeAny, From 7cd9b44031515e8a8b8d0db627a2cd98dd24f4cf Mon Sep 17 00:00:00 2001 From: Netanel Gilad Date: Sat, 19 Oct 2024 15:02:42 +0300 Subject: [PATCH 2/2] revert --- deno/lib/README.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/deno/lib/README.md b/deno/lib/README.md index cdae9403c..a721693b1 100644 --- a/deno/lib/README.md +++ b/deno/lib/README.md @@ -491,7 +491,6 @@ There are a growing number of tools that are built atop or support Zod natively! - [`sveltekit-superforms`](https://github.com/ciscoheat/sveltekit-superforms): Supercharged form library for SvelteKit with Zod validation. - [`mobx-zod-form`](https://github.com/MonoidDev/mobx-zod-form): Data-first form builder based on MobX & Zod. - [`@vee-validate/zod`](https://github.com/logaretm/vee-validate/tree/main/packages/zod): Form library for Vue.js with Zod schema validation. -- [`zod-form-renderer`](https://github.com/thepeaklab/zod-form-renderer): Auto-infer form fields from zod schema and render them with react-hook-form with E2E type safety. #### Zod to X @@ -506,7 +505,6 @@ There are a growing number of tools that are built atop or support Zod natively! - [`zod-openapi`](https://github.com/samchungy/zod-openapi): Create full OpenAPI v3.x documentation from Zod schemas. - [`fastify-zod-openapi`](https://github.com/samchungy/fastify-zod-openapi): Fastify type provider, validation, serialization and @fastify/swagger support for Zod schemas. - [`typeschema`](https://typeschema.com/): Universal adapter for schema validation. -- [`zodex`](https://github.com/commonbaseapp/zodex): (De)serialization for zod schemas #### X to Zod @@ -540,13 +538,11 @@ There are a growing number of tools that are built atop or support Zod natively! - [`freerstore`](https://github.com/JacobWeisenburger/freerstore): Firestore cost optimizer. - [`slonik`](https://github.com/gajus/slonik/tree/gajus/add-zod-validation-backwards-compatible#runtime-validation-and-static-type-inference): Node.js Postgres client with strong Zod integration. -- [`schemql`](https://github.com/a2lix/schemql): Enhances your SQL workflow by combining raw SQL with targeted type safety and schema validation. - [`soly`](https://github.com/mdbetancourt/soly): Create CLI applications with zod. - [`pastel`](https://github.com/vadimdemedes/pastel): Create CLI applications with react, zod, and ink. - [`zod-xlsx`](https://github.com/sidwebworks/zod-xlsx): A xlsx based resource validator using Zod schemas. - [`znv`](https://github.com/lostfictions/znv): Type-safe environment parsing and validation for Node.js with Zod schemas. - [`zod-config`](https://github.com/alexmarqs/zod-config): Load configurations across multiple sources with flexible adapters, ensuring type safety with Zod. -- [`unplugin-environment`](https://github.com/r17x/js/tree/main/packages/unplugin-environment#readme): A plugin for loading enviroment variables safely with schema validation, simple with virtual module, type-safe with intellisense, and better DX 🔥 🚀 👷. Powered by Zod. #### Utilities for Zod