Skip to content

Commit 8195fa0

Browse files
Merge pull request BitGo#167 from bitgopatmcl/simplify-intersections
feat: force simplification of intersection types in io-ts-http
2 parents b249de7 + 82b5e93 commit 8195fa0

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

packages/io-ts-http/src/combinators.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
OptionalizedC,
1111
OptionalProps,
1212
RequiredProps,
13+
Simplify,
1314
} from './utils';
1415

1516
export const optional = <C extends t.Mixed>(subCodec: C) =>
@@ -39,7 +40,10 @@ export const optionalized = <P extends t.Props>(props: P): OptionalizedC<P> => {
3940
export const flattened = <Props extends NestedProps>(
4041
name: string,
4142
props: Props,
42-
): t.Type<Flattened<NestedType<Props>>, NestedOutputType<Props>> => {
43+
): t.Type<
44+
Simplify<Flattened<NestedType<Props>>>,
45+
Simplify<NestedOutputType<Props>>
46+
> => {
4347
let flatProps: t.Props = {};
4448
for (const key in props) {
4549
if (!props.hasOwnProperty(key)) {
@@ -69,7 +73,7 @@ export const flattened = <Props extends NestedProps>(
6973
}
7074
flattened = { ...flattened, ...nested[key] };
7175
}
72-
return flattened as Flattened<NestedType<Props>>;
76+
return flattened as Simplify<Flattened<NestedType<Props>>>;
7377
}),
7478
),
7579
(input: any) => {
@@ -86,7 +90,7 @@ export const flattened = <Props extends NestedProps>(
8690
}
8791
}
8892
}
89-
return nested as NestedOutputType<Props>;
93+
return nested as Simplify<NestedOutputType<Props>>;
9094
},
9195
);
9296
};

packages/io-ts-http/src/utils.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ export type PossiblyUndefinedProps<T extends t.Props> = {
88
[K in keyof T]: undefined extends t.TypeOf<T[K]> ? K : never;
99
}[keyof T];
1010

11-
type Optionalized<T> = Omit<T, PossiblyUndefinedKeys<T>> &
12-
Partial<Pick<T, PossiblyUndefinedKeys<T>>>;
11+
type Optionalized<T> = Simplify<
12+
Omit<T, PossiblyUndefinedKeys<T>> & Partial<Pick<T, PossiblyUndefinedKeys<T>>>
13+
>;
1314

1415
export type OptionalProps<Props extends t.Props> = Pick<
1516
Props,
@@ -48,3 +49,5 @@ type UnionToIntersection<T> = (T extends any ? (x: T) => any : never) extends (
4849
) => any
4950
? R
5051
: never;
52+
53+
export type Simplify<T> = T extends unknown ? { [K in keyof T]: T[K] } : never;

0 commit comments

Comments
 (0)