-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add QueryResultOptionalPopulated helper type
- Loading branch information
Showing
3 changed files
with
23 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
import type { Entity } from '../Entity'; | ||
|
||
export type EntityPrimitiveOrId<T extends Entity> = Extract<NonNullable<T>, Entity> extends Entity ? Exclude<NonNullable<T>, Entity> | Pick<T, 'id'> : T; | ||
export type EntityPrimitiveOrId<T> = T extends [] | ||
? T extends (infer U)[] | ||
? EntityPrimitiveOrId<U>[] | ||
: T // Unable to determine array type, so return original | ||
: Extract<NonNullable<T>, Entity> extends undefined | ||
? T | ||
: Exclude<NonNullable<T>, Entity> | Pick<Extract<NonNullable<T>, Entity>, 'id'>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import type { Entity } from '../Entity'; | ||
|
||
import type { EntityPrimitiveOrId } from './EntityPrimitiveOrId'; | ||
import type { QueryResult } from './QueryResult'; | ||
|
||
/** | ||
* Allows a QueryResult type with specific properties optionally populated. If the property is populated, only the id property is needed | ||
*/ | ||
export type QueryResultOptionalPopulated<T extends Entity, K extends keyof T> = Omit<QueryResult<T>, K> & { | ||
[P in K]-?: T[P] extends [] | ||
? undefined extends T[P] // If property is not an array | ||
? EntityPrimitiveOrId<T[P]> | null // If the property is originally optional, include null as a possible value type | ||
: EntityPrimitiveOrId<T[P]> // Otherwise, use the TPropertyType | ||
: EntityPrimitiveOrId<T[P]>; // Otherwise return an array of items | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters