fix(types/Column): infer lazy-loaded relations (#584)

This commit updates the Column type to properly infer the relation properties of entities that use lazy loading, instead of inferring the properties of a Promise. This resolves an issue where the Column type would throw type errors when trying to access properties of the relation.
This commit is contained in:
IZEM 2023-04-13 06:41:59 +00:00 committed by GitHub
parent 1d422a0db3
commit 1b2106e13b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -9,6 +9,12 @@ type Join<K, P> = K extends string
type Prev = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...0[]]
// unwrap Promise type
type UnwrapPromise<T> = T extends Promise<infer U> ? UnwrapPromise<U> : T
// Unwrap Array type
type UnwrapArray<T> = T extends Array<infer U> ? UnwrapArray<U> : T
// TODO: puts some comments here, in this ternary of doom
export type Column<T, D extends number = 2> = [D] extends [never]
? never
@ -18,7 +24,11 @@ export type Column<T, D extends number = 2> = [D] extends [never]
? T[K] extends Date
? `${K}`
: T[K] extends Array<infer U>
? `${K}` | Join<K, Column<U, Prev[D]>>
? `${K}` | Join<K, Column<UnwrapArray<U>, Prev[D]>>
: T[K] extends Promise<infer U>
? U extends Array<infer V>
? `${K}` | Join<K, Column<UnwrapArray<V>, Prev[D]>>
: `${K}` | Join<K, Column<UnwrapPromise<U>, Prev[D]>>
: `${K}` | Join<K, Column<T[K], Prev[D]>>
: never
}[keyof T]