Skip to content

Commit 8ea4eae

Browse files
committed
make all partialBy HKTs extend a base hkt interface
1 parent f8120cb commit 8ea4eae

File tree

7 files changed

+53
-37
lines changed

7 files changed

+53
-37
lines changed

library/src/methods/partialBy/partialBy.test-d.ts

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,15 @@ import {
1313
string,
1414
type StringIssue,
1515
} from '../../schemas/index.ts';
16+
import type { BaseHKTable, HKTImplementation } from '../../types/hkt.ts';
1617
import type { InferInput, InferIssue, InferOutput } from '../../types/index.ts';
17-
import { partialBy, type SchemaWithPartialBy } from './partialBy.ts';
18+
import {
19+
partialBy,
20+
type PartialByModifierHKT,
21+
type SchemaWithPartialBy,
22+
} from './partialBy.ts';
23+
24+
type ModifierImpl = HKTImplementation<BaseHKTable<PartialByModifierHKT>>;
1825

1926
describe('partialBy', () => {
2027
const entries = {
@@ -27,10 +34,16 @@ describe('partialBy', () => {
2734
describe('object', () => {
2835
const wrapped = object(entries);
2936
type Wrapped = typeof wrapped;
30-
type Schema1<Modifier extends typeof optional | typeof nullish> =
31-
SchemaWithPartialBy<Wrapped, ReturnType<Modifier>, undefined>;
32-
type Schema2<Modifier extends typeof optional | typeof nullish> =
33-
SchemaWithPartialBy<Wrapped, ReturnType<Modifier>, ['key1', 'key3']>;
37+
type Schema1<TModifier extends ModifierImpl> = SchemaWithPartialBy<
38+
Wrapped,
39+
ReturnType<TModifier>,
40+
undefined
41+
>;
42+
type Schema2<TModifier extends ModifierImpl> = SchemaWithPartialBy<
43+
Wrapped,
44+
ReturnType<TModifier>,
45+
['key1', 'key3']
46+
>;
3447

3548
describe('should return schema object', () => {
3649
test('with modified keys', () => {
@@ -125,10 +138,16 @@ describe('partialBy', () => {
125138
describe('objectWithRest', () => {
126139
const wrapped = objectWithRest(entries, boolean());
127140
type Wrapped = typeof wrapped;
128-
type Schema1<Modifier extends typeof optional | typeof nullish> =
129-
SchemaWithPartialBy<Wrapped, ReturnType<Modifier>, undefined>;
130-
type Schema2<Modifier extends typeof optional | typeof nullish> =
131-
SchemaWithPartialBy<Wrapped, ReturnType<Modifier>, ['key1', 'key3']>;
141+
type Schema1<TModifier extends ModifierImpl> = SchemaWithPartialBy<
142+
Wrapped,
143+
ReturnType<TModifier>,
144+
undefined
145+
>;
146+
type Schema2<TModifier extends ModifierImpl> = SchemaWithPartialBy<
147+
Wrapped,
148+
ReturnType<TModifier>,
149+
['key1', 'key3']
150+
>;
132151

133152
describe('should return schema object', () => {
134153
test('with modified keys', () => {

library/src/methods/partialBy/partialBy.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,19 @@ type Schema = SchemaWithoutPipe<
5555
*
5656
* partialBy: (schema: BaseSchema<unknown, unknown, BaseIssue<unknown>>) => BaseSchema<unknown, unknown, BaseIssue<unknown>>
5757
*/
58-
interface ModifierHKT extends BaseHKT<'partialBy'> {
58+
export interface PartialByModifierHKT extends BaseHKT<'partialBy'> {
5959
argConstraint: [schema: BaseSchema<unknown, unknown, BaseIssue<unknown>>];
6060
result: BaseSchema<unknown, unknown, BaseIssue<unknown>>;
61+
62+
schema: this['args'][0];
6163
}
6264

6365
/**
6466
* Partial entries type.
6567
*/
6668
type PartialByEntries<
6769
TEntries extends ObjectEntries,
68-
TModifier extends BaseHKTable<ModifierHKT>,
70+
TModifier extends BaseHKTable<PartialByModifierHKT>,
6971
TKeys extends readonly (keyof TEntries)[] | undefined,
7072
> = {
7173
[TKey in keyof TEntries]: TKeys extends readonly (keyof TEntries)[]
@@ -80,7 +82,7 @@ type PartialByEntries<
8082
*/
8183
export type SchemaWithPartialBy<
8284
TSchema extends Schema,
83-
TModifier extends BaseHKTable<ModifierHKT>,
85+
TModifier extends BaseHKTable<PartialByModifierHKT>,
8486
TKeys extends ObjectKeys<TSchema> | undefined,
8587
> = TSchema extends
8688
| ObjectSchema<infer TEntries, ErrorMessage<ObjectIssue> | undefined>
@@ -270,7 +272,7 @@ export type SchemaWithPartialBy<
270272
*/
271273
export function partialBy<
272274
const TSchema extends Schema,
273-
const TModifier extends BaseHKTable<ModifierHKT>,
275+
const TModifier extends BaseHKTable<PartialByModifierHKT>,
274276
>(
275277
schema: TSchema,
276278
modifier: HKTImplementation<TModifier>
@@ -288,7 +290,7 @@ export function partialBy<
288290
export function partialBy<
289291
const TSchema extends Schema,
290292
const TKeys extends ObjectKeys<TSchema>,
291-
const TModifier extends BaseHKTable<ModifierHKT>,
293+
const TModifier extends BaseHKTable<PartialByModifierHKT>,
292294
>(
293295
schema: TSchema,
294296
modifier: HKTImplementation<TModifier>,
@@ -298,17 +300,17 @@ export function partialBy<
298300
// @__NO_SIDE_EFFECTS__
299301
export function partialBy(
300302
schema: Schema,
301-
modifier: HKTImplementation<BaseHKTable<ModifierHKT>>,
303+
modifier: HKTImplementation<BaseHKTable<PartialByModifierHKT>>,
302304
keys?: ObjectKeys<Schema>
303305
): SchemaWithPartialBy<
304306
Schema,
305-
BaseHKTable<ModifierHKT>,
307+
BaseHKTable<PartialByModifierHKT>,
306308
ObjectKeys<Schema> | undefined
307309
> {
308310
// Create modified object entries
309311
const entries: PartialByEntries<
310312
ObjectEntries,
311-
BaseHKTable<ModifierHKT>,
313+
BaseHKTable<PartialByModifierHKT>,
312314
ObjectKeys<Schema>
313315
> = {};
314316
for (const key in schema.entries) {

library/src/schemas/exactOptional/exactOptional.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { BaseHKT } from '../../types/hkt.ts';
1+
import type { PartialByModifierHKT } from '../../methods/partialBy/partialBy.ts';
22
import type {
33
BaseIssue,
44
BaseSchema,
@@ -9,9 +9,8 @@ import type {
99
} from '../../types/index.ts';
1010
import { _getStandardProps } from '../../utils/index.ts';
1111

12-
export interface ExactOptionalPartialHKT extends BaseHKT<'partialBy'> {
13-
argConstraint: [BaseSchema<unknown, unknown, BaseIssue<unknown>>];
14-
result: ExactOptionalSchema<this['args'][0], undefined>;
12+
export interface ExactOptionalPartialHKT extends PartialByModifierHKT {
13+
result: ExactOptionalSchema<this['schema'], undefined>;
1514
}
1615

1716
/**

library/src/schemas/nullable/nullable.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getDefault } from '../../methods/index.ts';
2-
import type { BaseHKT } from '../../types/hkt.ts';
2+
import type { PartialByModifierHKT } from '../../methods/partialBy/partialBy.ts';
33
import type {
44
BaseIssue,
55
BaseSchema,
@@ -11,9 +11,8 @@ import type {
1111
import { _getStandardProps } from '../../utils/index.ts';
1212
import type { InferNullableOutput } from './types.ts';
1313

14-
export interface NullablePartialHKT extends BaseHKT<'partialBy'> {
15-
argConstraint: [schema: BaseSchema<unknown, unknown, BaseIssue<unknown>>];
16-
result: NullableSchema<this['args'][0], undefined>;
14+
export interface NullablePartialHKT extends PartialByModifierHKT {
15+
result: NullableSchema<this['schema'], undefined>;
1716
}
1817

1918
/**

library/src/schemas/nullish/nullish.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getDefault } from '../../methods/index.ts';
2-
import type { BaseHKT } from '../../types/hkt.ts';
2+
import type { PartialByModifierHKT } from '../../methods/partialBy/partialBy.ts';
33
import type {
44
BaseIssue,
55
BaseSchema,
@@ -11,9 +11,8 @@ import type {
1111
import { _getStandardProps } from '../../utils/index.ts';
1212
import type { InferNullishOutput } from './types.ts';
1313

14-
export interface NullishPartialHKT extends BaseHKT<'partialBy'> {
15-
argConstraint: [schema: BaseSchema<unknown, unknown, BaseIssue<unknown>>];
16-
result: NullishSchema<this['args'][0], undefined>;
14+
export interface NullishPartialHKT extends PartialByModifierHKT {
15+
result: NullishSchema<this['schema'], undefined>;
1716
}
1817

1918
/**

library/src/schemas/optional/optional.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getDefault } from '../../methods/index.ts';
2-
import type { BaseHKT } from '../../types/hkt.ts';
2+
import type { PartialByModifierHKT } from '../../methods/partialBy/partialBy.ts';
33
import type {
44
BaseIssue,
55
BaseSchema,
@@ -11,9 +11,8 @@ import type {
1111
import { _getStandardProps } from '../../utils/index.ts';
1212
import type { InferOptionalOutput } from './types.ts';
1313

14-
export interface OptionalPartialHKT extends BaseHKT<'partialBy'> {
15-
argConstraint: [BaseSchema<unknown, unknown, BaseIssue<unknown>>];
16-
result: OptionalSchema<this['args'][0], undefined>;
14+
export interface OptionalPartialHKT extends PartialByModifierHKT {
15+
result: OptionalSchema<this['schema'], undefined>;
1716
}
1817

1918
/**

library/src/schemas/undefinedable/undefinedable.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getDefault } from '../../methods/index.ts';
2-
import type { BaseHKT } from '../../types/hkt.ts';
2+
import type { PartialByModifierHKT } from '../../methods/partialBy/partialBy.ts';
33
import type {
44
BaseIssue,
55
BaseSchema,
@@ -11,9 +11,8 @@ import type {
1111
import { _getStandardProps } from '../../utils/index.ts';
1212
import type { InferUndefinedableOutput } from './types.ts';
1313

14-
export interface UndefinedablePartialHKT extends BaseHKT<'partialBy'> {
15-
argConstraint: [schema: BaseSchema<unknown, unknown, BaseIssue<unknown>>];
16-
result: UndefinedableSchema<this['args'][0], undefined>;
14+
export interface UndefinedablePartialHKT extends PartialByModifierHKT {
15+
result: UndefinedableSchema<this['schema'], undefined>;
1716
}
1817

1918
/**

0 commit comments

Comments
 (0)