diff --git a/packages/uma/src/util/ReType.ts b/packages/uma/src/util/ReType.ts index 29e335f..4bbeb06 100644 --- a/packages/uma/src/util/ReType.ts +++ b/packages/uma/src/util/ReType.ts @@ -31,14 +31,22 @@ export type Assertion = (value: unknown) => asserts value is T; export type ReType = Literal | Assertion | { [_: PropertyKey]: ReType }; -export type Type = - R extends { [_: PropertyKey]: ReType } ? { - [K in keyof R as undefined extends Type ? never : K]: Type - } & { - [K in keyof R as undefined extends Type ? K : never]?: Type - } : +type _Expand = T extends infer O ? { [K in keyof O]: O[K] } : never; + +type _Required = { + [K in keyof R as undefined extends Type ? never : K]: Type +}; + +type _Optional = { + [K in keyof R as undefined extends Type ? K : never]?: Type +}; + +type _Type = + R extends { [_: PropertyKey]: ReType } ? _Required & _Optional : R extends Assertion ? T : R; + +export type Type = _Expand<_Type>; function isIn(key: PropertyKey, object: T): key is keyof T { return key in object; @@ -73,7 +81,6 @@ export function isType(value: unknown, assertion: R): value is return value === assertion; } - export const any: Assertion = () => {}; export const unknown: Assertion = () => {}; export const never: Assertion = () => { throw new Error() }; @@ -166,5 +173,3 @@ export const record = < export const dict = (records: T): Assertion>> => { return record(string, records); } - -// type Expand = T extends infer O ? { [K in keyof O]: O[K] } : never; // https://www.npmjs.com/package/type-expand