Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions library/src/schemas/intersect/intersect.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,17 @@ describe('intersect', () => {

test('of input', () => {
expectTypeOf<InferInput<Schema>>().toEqualTypeOf<
{ key1: string }[] & { key2?: number | undefined }[]
| ({ key1: string }[] & { key2?: number | undefined }[])
| { key1: string }[]
| { key2?: number | undefined }[]
>();
});

test('of output', () => {
expectTypeOf<InferOutput<Schema>>().toEqualTypeOf<
{ key1: string }[] & { key2: number }[]
| ({ key1: string }[] & { key2: number }[])
| { key1: string }[]
| { key2: number }[]
>();
});

Expand Down
12 changes: 11 additions & 1 deletion library/src/schemas/intersect/intersect.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getDefault } from '../../methods/index.ts';
import type {
BaseIssue,
BaseSchema,
Expand Down Expand Up @@ -105,7 +106,16 @@ export function intersect(

// Parse schema of each option and collect outputs
for (const schema of this.options) {
const optionDataset = schema['~run']({ value: input }, config);
let optionDataset = schema['~run']({ value: input }, config);
if (!optionDataset.typed) {
if (schema.type === 'optional') {
let result = getDefault(schema);
if (result === undefined) {
continue;
}
optionDataset = { typed: true, value: result };
}
}

// If there are issues, capture them
if (optionDataset.issues) {
Expand Down
8 changes: 6 additions & 2 deletions library/src/schemas/intersect/intersectAsync.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,17 @@ describe('intersectAsync', () => {

test('of input', () => {
expectTypeOf<InferInput<Schema>>().toEqualTypeOf<
{ key1: string }[] & { key2?: number | undefined }[]
| ({ key1: string }[] & { key2?: number | undefined }[])
| { key1: string }[]
| { key2?: number | undefined }[]
>();
});

test('of output', () => {
expectTypeOf<InferOutput<Schema>>().toEqualTypeOf<
{ key1: string }[] & { key2: number }[]
| ({ key1: string }[] & { key2: number }[])
| { key1: string }[]
| { key2: number }[]
>();
});

Expand Down
32 changes: 20 additions & 12 deletions library/src/schemas/intersect/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,16 @@ export type InferIntersectInput<
InferOption<infer TInput, unknown>,
...infer TRest,
]
? TRest extends readonly [
InferOption<unknown, unknown>,
...InferOption<unknown, unknown>[],
]
? TInput & InferIntersectInput<TRest>
: TInput
? TRest extends InferOption<unknown, unknown>[]
? TInput extends NonNullable<TInput>
?
| TInput
| InferIntersectInput<TRest>
| (TInput & InferIntersectInput<TRest>)
:
| InferIntersectInput<TRest>
| (NonNullable<TInput> & InferIntersectInput<TRest>)
: never
: never;

/**
Expand All @@ -73,10 +77,14 @@ export type InferIntersectOutput<
InferOption<unknown, infer TOutput>,
...infer TRest,
]
? TRest extends readonly [
InferOption<unknown, unknown>,
...InferOption<unknown, unknown>[],
]
? TOutput & InferIntersectOutput<TRest>
: TOutput
? TRest extends InferOption<unknown, unknown>[]
? TOutput extends NonNullable<TOutput>
?
| TOutput
| InferIntersectOutput<TRest>
| (TOutput & InferIntersectOutput<TRest>)
:
| InferIntersectOutput<TRest>
| (NonNullable<TOutput> & InferIntersectOutput<TRest>)
: never
: never;