Skip to content

feat: force simplification of intersection types in io-ts-http #167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 19, 2022
Merged
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
10 changes: 7 additions & 3 deletions packages/io-ts-http/src/combinators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
OptionalizedC,
OptionalProps,
RequiredProps,
Simplify,
} from './utils';

export const optional = <C extends t.Mixed>(subCodec: C) =>
Expand Down Expand Up @@ -39,7 +40,10 @@ export const optionalized = <P extends t.Props>(props: P): OptionalizedC<P> => {
export const flattened = <Props extends NestedProps>(
name: string,
props: Props,
): t.Type<Flattened<NestedType<Props>>, NestedOutputType<Props>> => {
): t.Type<
Simplify<Flattened<NestedType<Props>>>,
Simplify<NestedOutputType<Props>>
> => {
let flatProps: t.Props = {};
for (const key in props) {
if (!props.hasOwnProperty(key)) {
Expand Down Expand Up @@ -69,7 +73,7 @@ export const flattened = <Props extends NestedProps>(
}
flattened = { ...flattened, ...nested[key] };
}
return flattened as Flattened<NestedType<Props>>;
return flattened as Simplify<Flattened<NestedType<Props>>>;
}),
),
(input: any) => {
Expand All @@ -86,7 +90,7 @@ export const flattened = <Props extends NestedProps>(
}
}
}
return nested as NestedOutputType<Props>;
return nested as Simplify<NestedOutputType<Props>>;
},
);
};
7 changes: 5 additions & 2 deletions packages/io-ts-http/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ export type PossiblyUndefinedProps<T extends t.Props> = {
[K in keyof T]: undefined extends t.TypeOf<T[K]> ? K : never;
}[keyof T];

type Optionalized<T> = Omit<T, PossiblyUndefinedKeys<T>> &
Partial<Pick<T, PossiblyUndefinedKeys<T>>>;
type Optionalized<T> = Simplify<
Omit<T, PossiblyUndefinedKeys<T>> & Partial<Pick<T, PossiblyUndefinedKeys<T>>>
>;

export type OptionalProps<Props extends t.Props> = Pick<
Props,
Expand Down Expand Up @@ -48,3 +49,5 @@ type UnionToIntersection<T> = (T extends any ? (x: T) => any : never) extends (
) => any
? R
: never;

export type Simplify<T> = T extends unknown ? { [K in keyof T]: T[K] } : never;