Skip to content

Commit

Permalink
fix(typings): improve support for Reference wrapper in FilterQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
B4nan committed Apr 12, 2020
1 parent aa3a087 commit 7497c45
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/typings.ts
Expand Up @@ -53,11 +53,12 @@ export type OperatorMap<T> = {
$re?: string;
};
export type StringProp<T> = T extends string ? string | RegExp : never;
export type EntityOrPrimary<T> = true extends IsScalar<T> ? never : DeepPartialEntity<T> | PartialEntity<T> | Primary<T> | T;
export type EntityOrPrimary<T> = true extends IsScalar<T> ? never : DeepPartialEntity<ReferencedEntity<T>> | PartialEntity<ReferencedEntity<T>> | Primary<ReferencedEntity<T>> | ReferencedEntity<T>;
export type CollectionItem<T> = T extends Collection<infer K> ? EntityOrPrimary<K> : never;
export type FilterValue<T> = T | OperatorMap<T> | StringProp<T> | OneOrArray<CollectionItem<T> | EntityOrPrimary<T>> | null;
export type ReferencedEntity<T> = T extends Reference<infer K> ? K : T;
export type FilterValue<T> = T | OperatorMap<T> | StringProp<T> | OneOrArray<CollectionItem<T> | EntityOrPrimary<T>> | null;
export type Query<T> = true extends IsEntity<T>
? { [K in keyof T]?: Query<T[K]> | FilterValue<T[K]> | null } | FilterValue<T>
? { [K in keyof T]?: Query<ReferencedEntity<T[K]>> | FilterValue<ReferencedEntity<T[K]>> | null } | FilterValue<ReferencedEntity<T>>
: T extends Collection<infer K>
? { [KK in keyof K]?: Query<K[KK]> | FilterValue<K[KK]> | null } | FilterValue<K>
: FilterValue<T>;
Expand Down
6 changes: 6 additions & 0 deletions tests/types.test.ts
Expand Up @@ -228,6 +228,12 @@ describe('check typings', () => {

let ok03: FilterQuery<FooParam2>;
ok03 = { bar: 1, baz: 2 };

let ok04: FilterQuery<Book2>;
ok04 = { publisher: 1 };
ok04 = { publisher: { name: 'name' } };
ok04 = { publisher: { name: /name/ } };
ok04 = { publisher: { name: { $like: 'name' } } };
});

// there is no way to test this currently, uncomment to check they all fail
Expand Down

0 comments on commit 7497c45

Please sign in to comment.