Skip to content

Commit e6de2a9

Browse files
chore: minor update of reType (#28)
Signed-off-by: Wouter Termont <wouter.termont@ugent.be>
1 parent a02ce9c commit e6de2a9

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

packages/uma/src/util/ReType.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,22 @@ export type Assertion<T> = (value: unknown) => asserts value is T;
3131

3232
export type ReType = Literal | Assertion<unknown> | { [_: PropertyKey]: ReType };
3333

34-
export type Type<R extends ReType> =
35-
R extends { [_: PropertyKey]: ReType } ? {
36-
[K in keyof R as undefined extends Type<R[K]> ? never : K]: Type<R[K]>
37-
} & {
38-
[K in keyof R as undefined extends Type<R[K]> ? K : never]?: Type<R[K]>
39-
} :
34+
type _Expand<T> = T extends infer O ? { [K in keyof O]: O[K] } : never;
35+
36+
type _Required<R extends { [_: PropertyKey]: ReType }> = {
37+
[K in keyof R as undefined extends Type<R[K]> ? never : K]: Type<R[K]>
38+
};
39+
40+
type _Optional<R extends { [_: PropertyKey]: ReType }> = {
41+
[K in keyof R as undefined extends Type<R[K]> ? K : never]?: Type<R[K]>
42+
};
43+
44+
type _Type<R extends ReType> =
45+
R extends { [_: PropertyKey]: ReType } ? _Required<R> & _Optional<R> :
4046
R extends Assertion<infer T> ? T :
4147
R;
48+
49+
export type Type<R extends ReType> = _Expand<_Type<R>>;
4250

4351
function isIn<T extends object>(key: PropertyKey, object: T): key is keyof T {
4452
return key in object;
@@ -73,7 +81,6 @@ export function isType<R extends ReType>(value: unknown, assertion: R): value is
7381
return value === assertion;
7482
}
7583

76-
7784
export const any: Assertion<any> = () => {};
7885
export const unknown: Assertion<unknown> = () => {};
7986
export const never: Assertion<never> = () => { throw new Error() };
@@ -166,5 +173,3 @@ export const record = <
166173
export const dict = <T extends ReType> (records: T): Assertion<NodeJS.Dict<Type<T>>> => {
167174
return record(string, records);
168175
}
169-
170-
// type Expand<T> = T extends infer O ? { [K in keyof O]: O[K] } : never; // https://www.npmjs.com/package/type-expand

0 commit comments

Comments
 (0)