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) => {