diff --git a/src/index.ts b/src/index.ts index c75f36b..f9f966f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -type PocketBaseOption = { +interface PocketBaseOption { fields?: string expand?: string sort?: string @@ -18,12 +18,14 @@ type FieldsArrayToUnion = T extends Array ? RemoveModifier type Option> = { [Key in keyof TSchema]: { key: Key - fields?: (keyof { - [K in keyof TSchema[Key] as TSchema[Key][K] extends string - ? `${K & string}${'' | Modifier}` - : K]: unknown - })[] - expand?: Expand>[] + fields?: Array< + keyof { + [K in keyof TSchema[Key] as TSchema[Key][K] extends string + ? `${K & string}${'' | Modifier}` + : K]: unknown + } + > + expand?: Array>> sort?: '@random' | `${'' | '+' | '-'}${keyof TSchema[Key] & string}` filter?: string @@ -39,24 +41,23 @@ type Expand< [Key in TKey]: { key: Key fields?: NonNullable extends Array | infer U - ? (keyof { - [K in keyof U as U[K] extends string - ? `${K & string}${'' | Modifier}` - : K]: unknown - })[] + ? Array< + { + [K in keyof U]: U[K] extends string ? `${K & string}${'' | Modifier}` : K + }[keyof U] + > : never expand?: NonNullable extends Array - ? Expand>[] + ? Array>> : NonNullable extends infer U extends TSchema[keyof TSchema] - ? Expand>[] + ? Array>> : 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 + // Build fails with this simpler syntax due to a bug in esbuild (fixed in v0.19.12 but dependency is not updated yet in tsup and vitest) // expand?: NonNullable extends // | Array // | infer U extends TSchema[keyof TSchema] - // ? Expand>[] + // ? Array>> // : never } }[TKey] @@ -87,7 +88,7 @@ type ResponseType< ProcessExpandArray // check if all items in "expand" array are optional -type AllOptional = { +type AllOptional> = { [Obj in T[number] as Obj['key']]: undefined extends TRelation[Obj['key']] ? true : false } extends Record ? true