From 5166cb642ef04167c2c01ec823dc7dbe1838412a Mon Sep 17 00:00:00 2001 From: technicalcandidate-stack Date: Fri, 30 Jan 2026 11:44:00 -0800 Subject: [PATCH] fix: guard Prettify against any/unknown types Without this guard, Prettify was producing { [x: string]: any } instead of preserving 'any'. This caused issues when using p.any() in object schemas. - Add conditional check: any extends T ? T : ... - Add type-level tests for any() inference --- src/index.ts | 6 +++--- src/predicates/any.spec.ts | 31 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 8cabd7b..c12d664 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ -export type Prettify = { - [K in keyof T]: T[K]; -} & {}; +export type Prettify = any extends T + ? T // preserve any/unknown + : { [K in keyof T]: T[K] } & {}; export type ValidationResult = { isValid: true value: T diff --git a/src/predicates/any.spec.ts b/src/predicates/any.spec.ts index 1b961ca..fcf6f2b 100644 --- a/src/predicates/any.spec.ts +++ b/src/predicates/any.spec.ts @@ -1,5 +1,36 @@ import {test} from 'kizu'; import {any} from './any'; +import {object, string} from '.'; +import type {Infer} from '..'; + +// Type-level test: Infer should be `any`, not `{ [x: string]: any }` +// eslint-disable-next-line @typescript-eslint/no-unused-vars +const anyValidator = any(); + +type AnyType = Infer; + +// eslint-disable-next-line @typescript-eslint/no-unused-vars, no-underscore-dangle +const anyTest1: AnyType = 'string'; // should work - any accepts string +// eslint-disable-next-line @typescript-eslint/no-unused-vars, no-underscore-dangle +const anyTest2: AnyType = 42; // should work - any accepts number +// eslint-disable-next-line @typescript-eslint/no-unused-vars, no-underscore-dangle +const anyTest3: AnyType = {foo: 'bar'}; // should work - any accepts object + +// Type-level test: any() in object schema should allow any value +// eslint-disable-next-line @typescript-eslint/no-unused-vars +const schema = object({ + name: string(), + metadata: any(), +}); + +type Schema = Infer; + +// eslint-disable-next-line @typescript-eslint/no-unused-vars, no-underscore-dangle +const schemaTest1: Schema = {name: 'test', metadata: 'anything'}; +// eslint-disable-next-line @typescript-eslint/no-unused-vars, no-underscore-dangle +const schemaTest2: Schema = {name: 'test', metadata: 123}; +// eslint-disable-next-line @typescript-eslint/no-unused-vars, no-underscore-dangle +const schemaTest3: Schema = {name: 'test', metadata: {nested: 'object'}}; test('any(): accepts any value', (assert) => {