From a8a8c500e57c0fb1a6eefa007eec042bd14b4ebb Mon Sep 17 00:00:00 2001 From: Satoshi Date: Wed, 3 Jan 2024 02:12:00 +0900 Subject: [PATCH] fix compile errors --- src/index.ts | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/src/index.ts b/src/index.ts index 7675d2f..327e006 100644 --- a/src/index.ts +++ b/src/index.ts @@ -18,6 +18,7 @@ type Option> : K]: unknown })[] expand?: Expand>[] + sort?: '@random' | `${'' | '+' | '-'}${keyof TSchema[Key] & string}` filter?: string requestKey?: string @@ -31,21 +32,37 @@ type Expand< > = { [Key in TKey]: { key: Key - fields?: Exclude extends Array | infer U + fields?: Required[Key] extends Array ? (keyof { [K in keyof U & string as U[K] extends string ? `${K}${'' | Modifier}` : K]: unknown })[] - : never - // fields?: Exclude extends Array | infer U - // ? (keyof U)[] - // : never - expand?: Exclude extends - | Array - | infer U extends TSchema[keyof TSchema] + : (keyof { + [K in keyof TRelation[Key] & string as TRelation[Key][K] extends string + ? `${K}${'' | Modifier}` + : K]: unknown + })[] + expand?: Required[Key] extends Array ? Expand>[] - : never + : Required[Key] extends infer U extends TSchema[keyof TSchema] + ? Expand>[] + : never + + // the following is correct syntax and passes tests, but fails at build step + // infer within union doesn't seem to work for whatever reason + // fields?: Required[Key] extends Array | infer U + // ? (keyof { + // [K in keyof U & string as U[K] extends string + // ? `${K}${'' | Modifier}` + // : K]: unknown + // })[] + // : never + // expand?: Required[Key] extends + // | Array + // | infer U extends TSchema[keyof TSchema] + // ? Expand>[] + // : never } }[TKey] @@ -60,7 +77,7 @@ type BackRelation = keyof ? Key : never : never - : never]: {} + : never]: unknown } & keyof TRelation @@ -69,7 +86,6 @@ type ResponseType< TRelation extends BaseRelation, TOption extends Option, _Obj = TSchema[TOption['key']] - // > = TOption['fields'] extends Array > = TOption['fields'] extends Array ? RemoveModifier extends infer Fields extends keyof _Obj ? Pick<_Obj, Fields> & ProcessExpandArray @@ -131,10 +147,8 @@ type ProcessSingleExpand< TSchema extends BaseSchema, TRelation extends BaseRelation, TExpand extends Expand, - _Obj = Exclude extends Array | infer U - ? U - : never, - _IsToMany extends boolean = Exclude extends Array + _Obj = Required[TExpand['key']] extends Array | infer U ? U : never, + _IsToMany extends boolean = Required[TExpand['key']] extends Array ? true : false > = TExpand['fields'] extends Array