Skip to content

Commit

Permalink
fix where clause unable to allow all possible key especially from dis…
Browse files Browse the repository at this point in the history
…criminated union
  • Loading branch information
tylim88 committed Mar 10, 2024
1 parent 694a55c commit 9d4f036
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 25 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "firelordjs",
"version": "2.8.0",
"version": "2.8.1",
"description": "🔥 High Precision Typescript Wrapper for Firestore Web, Providing Unparalleled Type Safe and Dev Experience",
"author": "tylim",
"license": "MIT",
Expand Down
1 change: 1 addition & 0 deletions src/types/metaTypeCreator/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './metaTypeCreator'
export * from './metaType'
export * from './abstract'
export * from './utils'
6 changes: 6 additions & 0 deletions src/types/metaTypeCreator/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { MetaType } from './metaType'

// TODO add tests!
export type GetAllCompareKeys<T extends MetaType> =

Check warning on line 4 in src/types/metaTypeCreator/utils.ts

View workflow job for this annotation

GitHub Actions / build_publish

'T' is defined but never used. Allowed unused vars must match /^_/u
(T['compare'] extends infer T ? (T extends T ? keyof T : never) : never) &
string
4 changes: 2 additions & 2 deletions src/types/queryConstraints/orderBy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { OrderByDirection, QueryOrderByConstraint } from '../alias'
import { MetaType } from '../metaTypeCreator'
import { MetaType, GetAllCompareKeys } from '../metaTypeCreator'
import { __name__ } from '../fieldPath'

export type OrderByConstraint<FieldPath extends string> = {
Expand All @@ -10,7 +10,7 @@ export type OrderByConstraint<FieldPath extends string> = {

export type OrderBy = <
T extends MetaType,
FieldPath extends (keyof T['compare'] & string) | __name__,
FieldPath extends GetAllCompareKeys<T> | __name__,
DirectionStr extends OrderByDirection | undefined = undefined
>(
fieldPath: FieldPath,
Expand Down
8 changes: 4 additions & 4 deletions src/types/queryConstraints/query.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MetaType } from '../metaTypeCreator'
import { MetaType, GetAllCompareKeys } from '../metaTypeCreator'
import { WhereFilterOp } from '../alias'
import { CursorType, CursorConstraint } from './cursor'
import { WhereConstraint } from './where'
Expand All @@ -9,14 +9,14 @@ import { QueryCompositeFilterConstraint } from './composite'
type QueryNonFilterConstraints<T extends MetaType> =
| LimitConstraint<'limit' | 'limitToLast'>
| CursorConstraint<CursorType, unknown[]>
| OrderByConstraint<keyof T['compare'] & string>
| OrderByConstraint<GetAllCompareKeys<T>>

export type QueryConstraints<T extends MetaType> =
| WhereConstraint<T, keyof T['compare'] & string, WhereFilterOp, unknown>
| WhereConstraint<T, GetAllCompareKeys<T>, WhereFilterOp, unknown>
| QueryNonFilterConstraints<T>

export type QueryFilterConstraints<T extends MetaType> =
| WhereConstraint<T, keyof T['compare'] & string, WhereFilterOp, unknown>
| WhereConstraint<T, GetAllCompareKeys<T>, WhereFilterOp, unknown>
| QueryCompositeFilterConstraint<T, 'and' | 'or', QueryFilterConstraints<T>[]>

export type QueryAllConstraints<T extends MetaType> =
Expand Down
6 changes: 3 additions & 3 deletions src/types/queryConstraints/where.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { MetaType } from '../metaTypeCreator'
import { MetaType, GetAllCompareKeys } from '../metaTypeCreator'
import { WhereFilterOp, QueryFieldFilterConstraint } from '../alias'
import { __name__ } from '../fieldPath'

export type WhereConstraint<
T extends MetaType,
FieldPath extends keyof T['compare'] & string,
FieldPath extends GetAllCompareKeys<T> | __name__,
OpStr extends WhereFilterOp,
Value
> = {
Expand All @@ -17,7 +17,7 @@ export type WhereConstraint<

export type Where = <
T extends MetaType,
FieldPath extends (keyof T['compare'] & string) | __name__,
FieldPath extends GetAllCompareKeys<T> | __name__,
OpStr extends WhereFilterOp,
const Value
>(
Expand Down
4 changes: 2 additions & 2 deletions src/types/queryConstraintsLimitations/composite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export type ValidateTopLevelQueryCompositeFilterPartOne<
T extends MetaType,
AllQQCs extends readonly QueryAllConstraints<T>[]
> = AllQQCs extends (infer P)[]
? Extract<P, WhereConstraint<T, string, WhereFilterOp, unknown>> extends never
? Extract<P, WhereConstraint<T, any, WhereFilterOp, unknown>> extends never

Check warning on line 54 in src/types/queryConstraintsLimitations/composite.ts

View workflow job for this annotation

GitHub Actions / build_publish

Unexpected any. Specify a different type
? true
: GetAllQueryFilterCompositeConstraint<T, AllQQCs, never> extends never
? true
Expand Down Expand Up @@ -129,7 +129,7 @@ export type QueryFilterConstraintLimitation<
| OrderByConstraint<string>
| CursorConstraint<CursorType, unknown[]>
? ErrorOrAndInvalidConstraints
: Head extends WhereConstraint<T, string, WhereFilterOp, unknown>
: Head extends WhereConstraint<T, any, WhereFilterOp, unknown>

Check warning on line 132 in src/types/queryConstraintsLimitations/composite.ts

View workflow job for this annotation

GitHub Actions / build_publish

Unexpected any. Specify a different type
? Head['opStr'] extends NotIn
? 'or' extends ParentConstraint['type']
? ParentConstraint['constraints']['length'] extends 1
Expand Down
4 changes: 2 additions & 2 deletions src/types/queryConstraintsLimitations/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export type ValidateOrderByAndInequalityWhere<
AllQCs extends readonly QueryConstraints<T>[]
> = GetFirstInequalityWhere<T, AllQCs> extends infer W extends WhereConstraint<
T,
string,
any,

Check warning on line 37 in src/types/queryConstraintsLimitations/query.ts

View workflow job for this annotation

GitHub Actions / build_publish

Unexpected any. Specify a different type
InequalityOpStr,
unknown
>
Expand Down Expand Up @@ -62,7 +62,7 @@ export type QueryConstraintLimitation<
? Head
: Head extends LimitConstraint<'limitToLast'>
? LimitToLastConstraintLimitation<T, Head, AllQCs>
: Head extends WhereConstraint<T, string, WhereFilterOp, unknown>
: Head extends WhereConstraint<T, any, WhereFilterOp, unknown>

Check warning on line 65 in src/types/queryConstraintsLimitations/query.ts

View workflow job for this annotation

GitHub Actions / build_publish

Unexpected any. Specify a different type
? ValidateWhereArrayContainsArrayContainsAny<
T,
Head,
Expand Down
22 changes: 11 additions & 11 deletions src/types/queryConstraintsLimitations/where.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { DeepValue } from '../objectFlatten'
// You can't combine 'not-in' with 'or', 'in', 'array-contains-any', or '!=' in the same query.
type ValidateWhereNotIn<
T extends MetaType,
U extends WhereConstraint<T, string, WhereFilterOp, unknown>,
U extends WhereConstraint<T, any, WhereFilterOp, unknown>,

Check warning on line 33 in src/types/queryConstraintsLimitations/where.ts

View workflow job for this annotation

GitHub Actions / build_publish

Unexpected any. Specify a different type
PreviousQCs extends readonly QueryConstraints<T>[]
> = U['opStr'] extends NotIn
? Extract<
Expand All @@ -51,7 +51,7 @@ type ValidateWhereNotIn<
// You cannot use more than one '!=' filter. (undocumented)
type ValidateWhereNotEqual<
T extends MetaType,
U extends WhereConstraint<T, string, WhereFilterOp, unknown>,
U extends WhereConstraint<T, any, WhereFilterOp, unknown>,

Check warning on line 54 in src/types/queryConstraintsLimitations/where.ts

View workflow job for this annotation

GitHub Actions / build_publish

Unexpected any. Specify a different type
PreviousQCs extends readonly QueryConstraints<T>[]
> = U['opStr'] extends NotEqual
? Extract<
Expand All @@ -65,7 +65,7 @@ type ValidateWhereNotEqual<
// You can use at most one array-contains or array-contains-any clause per query. You can't combine array-contains with array-contains-any.
export type ValidateWhereArrayContainsArrayContainsAny<
T extends MetaType,
U extends WhereConstraint<T, string, WhereFilterOp, unknown>,
U extends WhereConstraint<T, any, WhereFilterOp, unknown>,

Check warning on line 68 in src/types/queryConstraintsLimitations/where.ts

View workflow job for this annotation

GitHub Actions / build_publish

Unexpected any. Specify a different type
PreviousQCs extends readonly QueryConstraints<T>[]
> = U['opStr'] extends ArrayContains
? Extract<
Expand All @@ -86,18 +86,18 @@ export type ValidateWhereArrayContainsArrayContainsAny<
// In a compound query, range (<, <=, >, >=) and not equals (!=, not-in) comparisons must all filter on the same field.
type ValidateWhereInequalityOpStrSameField<
T extends MetaType,
U extends WhereConstraint<T, string, WhereFilterOp, unknown>,
U extends WhereConstraint<T, any, WhereFilterOp, unknown>,

Check warning on line 89 in src/types/queryConstraintsLimitations/where.ts

View workflow job for this annotation

GitHub Actions / build_publish

Unexpected any. Specify a different type
PreviousQCs extends readonly QueryConstraints<T>[]
> = U['opStr'] extends InequalityOpStr
? Extract<
GetAllWhereConstraint<T, PreviousQCs, never>,
WhereConstraint<T, string, InequalityOpStr, unknown>
WhereConstraint<T, any, InequalityOpStr, unknown>

Check warning on line 94 in src/types/queryConstraintsLimitations/where.ts

View workflow job for this annotation

GitHub Actions / build_publish

Unexpected any. Specify a different type
> extends never
? true
: Exclude<
Extract<
GetAllWhereConstraint<T, PreviousQCs, never>,
WhereConstraint<T, string, InequalityOpStr, unknown>
WhereConstraint<T, any, InequalityOpStr, unknown>
>,
WhereConstraint<T, U['fieldPath'], InequalityOpStr, unknown>
> extends never
Expand All @@ -109,23 +109,23 @@ export type GetFirstInequalityWhere<
T extends MetaType,
QCs extends readonly QueryConstraints<T>[]
> = QCs extends [infer H, ...infer Rest extends readonly QueryConstraints<T>[]]
? H extends WhereConstraint<T, string, InequalityOpStr, unknown>
? H extends WhereConstraint<T, any, InequalityOpStr, unknown>
? H
: GetFirstInequalityWhere<T, Rest>
: true // not found, no check needed

export type GetAllWhereConstraint<
T extends MetaType,
AllQCs extends readonly QueryConstraints<T>[],
WhereConstraintsAcc extends WhereConstraint<T, string, WhereFilterOp, unknown>
WhereConstraintsAcc extends WhereConstraint<T, any, WhereFilterOp, unknown>
> = AllQCs extends [infer H, ...infer R]
? R extends readonly QueryConstraints<T>[]
?
| WhereConstraintsAcc
| GetAllWhereConstraint<
T,
R,
| (H extends WhereConstraint<T, string, WhereFilterOp, unknown>
| (H extends WhereConstraint<T, any, WhereFilterOp, unknown>
? H
: never)
| WhereConstraintsAcc
Expand All @@ -144,7 +144,7 @@ type GetAllWhereConstraintOpStr<
| GetAllWhereConstraintOpStr<
T,
R,
| (H extends WhereConstraint<T, string, WhereFilterOp, unknown>
| (H extends WhereConstraint<T, any, WhereFilterOp, unknown>
? H['opStr']
: never)
| OpStrAcc
Expand All @@ -155,7 +155,7 @@ type GetAllWhereConstraintOpStr<
export type WhereConstraintLimitation<
T extends MetaType,
Q extends GeneralQuery<T>,
U extends WhereConstraint<T, string, WhereFilterOp, unknown>,
U extends WhereConstraint<T, any, WhereFilterOp, unknown>,
PreviousQCs extends readonly QueryConstraints<T>[]
> = ValidateWhereNotIn<T, U, PreviousQCs> extends infer R extends string
? R
Expand Down

0 comments on commit 9d4f036

Please sign in to comment.