Skip to content

Commit e3f9231

Browse files
committed
fix where clause unable to allow all possible key especially from discriminated union
1 parent 22651e4 commit e3f9231

File tree

10 files changed

+85
-37
lines changed

10 files changed

+85
-37
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "firelord",
3-
"version": "2.8.0",
3+
"version": "2.8.1",
44
"description": "🔥 Write V9 like Firestore Admin code with extreme type safety.",
55
"author": "tylim",
66
"license": "MIT",

src/types/metaTypeCreator/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './metaTypeCreator'
22
export * from './metaType'
33
export * from './abstract'
4+
export * from './utils'

src/types/metaTypeCreator/utils.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { MetaType } from './metaType'
2+
3+
// TODO add tests!
4+
export type GetAllCompareKeys<T extends MetaType> =
5+
(T['compare'] extends infer T ? (T extends T ? keyof T : never) : never) &
6+
string

src/types/queryConstraints/orderBy.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { OrderByDirection } from '../alias'
2-
import { MetaType } from '../metaTypeCreator'
2+
import { MetaType, GetAllCompareKeys } from '../metaTypeCreator'
33

44
export type OrderByConstraint<
5-
FieldPath extends string,
5+
T extends MetaType,
6+
FieldPath extends GetAllCompareKeys<T>,
67
DirectionStr extends OrderByDirection | undefined = undefined
78
> = {
89
type: 'orderBy'
@@ -12,9 +13,9 @@ export type OrderByConstraint<
1213

1314
export type OrderBy = <
1415
T extends MetaType,
15-
FieldPath extends keyof T['compare'] & string,
16+
FieldPath extends GetAllCompareKeys<T>,
1617
DirectionStr extends OrderByDirection | undefined = undefined
1718
>(
1819
fieldPath: FieldPath,
1920
directionStr?: DirectionStr
20-
) => OrderByConstraint<FieldPath, DirectionStr>
21+
) => OrderByConstraint<T, FieldPath, DirectionStr>

src/types/queryConstraints/query.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import { CursorType, CursorConstraint } from './cursor'
22
import { WhereConstraint } from './where'
33
import { WhereFilterOp, OrderByDirection } from '../alias'
44
import { LimitConstraint } from './limit'
5-
import { MetaType } from '../metaTypeCreator'
5+
import { MetaType, GetAllCompareKeys } from '../metaTypeCreator'
66
import { OrderByConstraint } from './orderBy'
77
import { OffsetConstraint } from './offset'
88

99
export type QueryConstraints<T extends MetaType> =
10-
| WhereConstraint<T, keyof T['compare'] & string, WhereFilterOp, unknown>
10+
| WhereConstraint<T, GetAllCompareKeys<T>, WhereFilterOp, unknown>
1111
| LimitConstraint<'limit' | 'limitToLast', number>
1212
| CursorConstraint<CursorType, unknown[]>
13-
| OrderByConstraint<keyof T['compare'] & string, OrderByDirection | undefined>
13+
| OrderByConstraint<T, GetAllCompareKeys<T>, OrderByDirection | undefined>
1414
| OffsetConstraint

src/types/queryConstraints/where.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { MetaType } from '../metaTypeCreator'
1+
import { MetaType, GetAllCompareKeys } from '../metaTypeCreator'
22
import { WhereFilterOp } from '../alias'
33
import { __name__ } from '../fieldPath'
44

55
export type WhereConstraint<
66
T extends MetaType,
7-
FieldPath extends keyof T['compare'] & string,
7+
FieldPath extends GetAllCompareKeys<T> | __name__,
88
OpStr extends WhereFilterOp,
99
Value
1010
> = {
@@ -16,7 +16,7 @@ export type WhereConstraint<
1616

1717
export type Where = <
1818
T extends MetaType,
19-
FieldPath extends (keyof T['compare'] & string) | __name__,
19+
FieldPath extends GetAllCompareKeys<T> | __name__,
2020
OpStr extends WhereFilterOp,
2121
const Value
2222
>(

src/types/queryConstraintsLimitations/cursor.ts

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { MetaType } from '../metaTypeCreator'
1+
import { MetaType, GetAllCompareKeys } from '../metaTypeCreator'
22
import { OrderByDirection } from '../alias'
33
import { ErrorCursorTooManyArguments } from '../error'
44
import {
@@ -18,10 +18,18 @@ type ValidateCursorOrderBy<
1818
T extends MetaType,
1919
Q extends Query<T>,
2020
Values extends unknown[],
21-
AllOrderBy extends OrderByConstraint<string, OrderByDirection | undefined>[]
21+
AllOrderBy extends OrderByConstraint<
22+
T,
23+
GetAllCompareKeys<T>,
24+
OrderByDirection | undefined
25+
>[]
2226
> = Values extends [infer Head, ...infer Rest]
2327
? AllOrderBy extends [infer H, ...infer R]
24-
? H extends OrderByConstraint<string, OrderByDirection | undefined>
28+
? H extends OrderByConstraint<
29+
T,
30+
GetAllCompareKeys<T>,
31+
OrderByDirection | undefined
32+
>
2533
? [
2634
H['fieldPath'] extends __name__
2735
? GetCorrectDocumentIdBasedOnRef<T, Q, H['fieldPath'], Head>
@@ -38,7 +46,11 @@ type ValidateCursorOrderBy<
3846
T,
3947
Q,
4048
Rest,
41-
R extends OrderByConstraint<string, OrderByDirection | undefined>[]
49+
R extends OrderByConstraint<
50+
T,
51+
GetAllCompareKeys<T>,
52+
OrderByDirection | undefined
53+
>[]
4254
? R
4355
: []
4456
>

src/types/queryConstraintsLimitations/orderBy.ts

+26-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { MetaType } from '../metaTypeCreator'
1+
import { MetaType, GetAllCompareKeys } from '../metaTypeCreator'
22
import { OrderByDirection } from '../alias'
33
import { ErrorWhereOrderByEquality } from '../error'
44
import {
@@ -12,7 +12,11 @@ import { In, Equal } from './utils'
1212
// You can't order your query by a field included in an equality (==) or (in) clause.
1313
export type ValidateOrderByEqualityWhere<
1414
T extends MetaType,
15-
U extends OrderByConstraint<string, OrderByDirection | undefined>,
15+
U extends OrderByConstraint<
16+
T,
17+
GetAllCompareKeys<T>,
18+
OrderByDirection | undefined
19+
>,
1620
AllQCs extends QueryConstraints<T>[]
1721
> = Extract<
1822
GetAllWhereConstraint<T, AllQCs, never>,
@@ -23,7 +27,11 @@ export type ValidateOrderByEqualityWhere<
2327

2428
export type OrderByConstraintLimitation<
2529
T extends MetaType,
26-
U extends OrderByConstraint<string, OrderByDirection | undefined>,
30+
U extends OrderByConstraint<
31+
T,
32+
GetAllCompareKeys<T>,
33+
OrderByDirection | undefined
34+
>,
2735
AllQCs extends QueryConstraints<T>[]
2836
> = ValidateOrderByEqualityWhere<T, U, AllQCs> extends false
2937
? ErrorWhereOrderByEquality
@@ -33,7 +41,11 @@ export type GetFirstOrderBy<
3341
T extends MetaType,
3442
QCs extends QueryConstraints<T>[]
3543
> = QCs extends [infer H, ...infer Rest]
36-
? H extends OrderByConstraint<string, OrderByDirection | undefined>
44+
? H extends OrderByConstraint<
45+
T,
46+
GetAllCompareKeys<T>,
47+
OrderByDirection | undefined
48+
>
3749
? H
3850
: Rest extends QueryConstraints<T>[]
3951
? GetFirstOrderBy<T, Rest>
@@ -43,13 +55,21 @@ export type GetFirstOrderBy<
4355
export type GetAllOrderBy<
4456
T extends MetaType,
4557
QCs extends QueryConstraints<T>[],
46-
AllOrderBy extends OrderByConstraint<string, OrderByDirection | undefined>[]
58+
AllOrderBy extends OrderByConstraint<
59+
T,
60+
GetAllCompareKeys<T>,
61+
OrderByDirection | undefined
62+
>[]
4763
> = QCs extends [infer H, ...infer Rest]
4864
? Rest extends QueryConstraints<T>[]
4965
? GetAllOrderBy<
5066
T,
5167
Rest,
52-
H extends OrderByConstraint<string, OrderByDirection | undefined>
68+
H extends OrderByConstraint<
69+
T,
70+
GetAllCompareKeys<T>,
71+
OrderByDirection | undefined
72+
>
5373
? [...AllOrderBy, H]
5474
: AllOrderBy
5575
>

src/types/queryConstraintsLimitations/query.ts

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { MetaType } from '../metaTypeCreator'
1+
import { MetaType, GetAllCompareKeys } from '../metaTypeCreator'
22
import { WhereFilterOp, OrderByDirection } from '../alias'
33
import {
44
QueryConstraints,
@@ -23,9 +23,13 @@ export type ValidateOrderByAndInequalityWhere<
2323
T extends MetaType,
2424
AllQCs extends QueryConstraints<T>[]
2525
> = GetFirstInequalityWhere<T, AllQCs> extends infer W
26-
? W extends WhereConstraint<T, string, InequalityOpStr, unknown>
26+
? W extends WhereConstraint<T, any, InequalityOpStr, unknown>
2727
? GetFirstOrderBy<T, AllQCs> extends infer O
28-
? O extends OrderByConstraint<string, OrderByDirection | undefined>
28+
? O extends OrderByConstraint<
29+
T,
30+
GetAllCompareKeys<T>,
31+
OrderByDirection | undefined
32+
>
2933
? IsSame<W['fieldPath'], O['fieldPath']> extends true
3034
? true
3135
: ErrorWhereOrderByAndInEquality<O['fieldPath'], W['fieldPath']>
@@ -47,11 +51,15 @@ export type QueryConstraintLimitation<
4751
? [
4852
Head extends LimitConstraint<'limit', number> | OffsetConstraint
4953
? Head
50-
: Head extends OrderByConstraint<string, OrderByDirection | undefined>
54+
: Head extends OrderByConstraint<
55+
T,
56+
GetAllCompareKeys<T>,
57+
OrderByDirection | undefined
58+
>
5159
? OrderByConstraintLimitation<T, Head, AllQCs>
5260
: Head extends LimitConstraint<'limitToLast', number>
5361
? LimitToLastConstraintLimitation<T, Head, AllQCs>
54-
: Head extends WhereConstraint<T, string, WhereFilterOp, unknown>
62+
: Head extends WhereConstraint<T, any, WhereFilterOp, unknown>
5563
? WhereConstraintLimitation<T, Q, Head, PreviousQCs>
5664
: Head extends CursorConstraint<CursorType, unknown[]>
5765
? CursorConstraintLimitation<T, Q, Head, PreviousQCs>

src/types/queryConstraintsLimitations/where.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import { DeepValue } from '../objectFlatten'
3030
// You can use at most one in, not-in, or array-contains-any clause per query. You can't combine in , not-in, and array-contains-any in the same query.
3131
type ValidateWhereNotInArrayContainsAny<
3232
T extends MetaType,
33-
U extends WhereConstraint<T, string, WhereFilterOp, unknown>,
33+
U extends WhereConstraint<T, any, WhereFilterOp, unknown>,
3434
PreviousQCs extends QueryConstraints<T>[]
3535
> = U['opStr'] extends In | NotIn | ArrayContainsAny
3636
? Extract<
@@ -45,7 +45,7 @@ type ValidateWhereNotInArrayContainsAny<
4545
// You cannot use more than one '!=' filter. (not documented directly or indirectly)
4646
type ValidateWhereNotInNotEqual<
4747
T extends MetaType,
48-
U extends WhereConstraint<T, string, WhereFilterOp, unknown>,
48+
U extends WhereConstraint<T, any, WhereFilterOp, unknown>,
4949
PreviousQCs extends QueryConstraints<T>[]
5050
> = U['opStr'] extends NotIn
5151
? Extract<
@@ -71,7 +71,7 @@ type ValidateWhereNotInNotEqual<
7171
// You can use at most one array-contains clause per query. You can't combine array-contains with array-contains-any.
7272
type ValidateWhereArrayContainsArrayContainsAny<
7373
T extends MetaType,
74-
U extends WhereConstraint<T, string, WhereFilterOp, unknown>,
74+
U extends WhereConstraint<T, any, WhereFilterOp, unknown>,
7575
PreviousQCs extends QueryConstraints<T>[]
7676
> = U['opStr'] extends ArrayContains
7777
? Extract<
@@ -92,18 +92,18 @@ type ValidateWhereArrayContainsArrayContainsAny<
9292
// In a compound query, range (<, <=, >, >=) and not equals (!=, not-in) comparisons must all filter on the same field.
9393
type ValidateWhereInequalityOpStrSameField<
9494
T extends MetaType,
95-
U extends WhereConstraint<T, string, WhereFilterOp, unknown>,
95+
U extends WhereConstraint<T, any, WhereFilterOp, unknown>,
9696
PreviousQCs extends QueryConstraints<T>[]
9797
> = U['opStr'] extends InequalityOpStr
9898
? Extract<
9999
GetAllWhereConstraint<T, PreviousQCs, never>,
100-
WhereConstraint<T, string, InequalityOpStr, unknown>
100+
WhereConstraint<T, any, InequalityOpStr, unknown>
101101
> extends never
102102
? true
103103
: Exclude<
104104
Extract<
105105
GetAllWhereConstraint<T, PreviousQCs, never>,
106-
WhereConstraint<T, string, InequalityOpStr, unknown>
106+
WhereConstraint<T, any, InequalityOpStr, unknown>
107107
>,
108108
WhereConstraint<T, U['fieldPath'], InequalityOpStr, unknown>
109109
> extends never
@@ -115,7 +115,7 @@ export type GetFirstInequalityWhere<
115115
T extends MetaType,
116116
QCs extends QueryConstraints<T>[]
117117
> = QCs extends [infer H, ...infer Rest]
118-
? H extends WhereConstraint<T, string, InequalityOpStr, unknown>
118+
? H extends WhereConstraint<T, any, InequalityOpStr, unknown>
119119
? H
120120
: Rest extends QueryConstraints<T>[]
121121
? GetFirstInequalityWhere<T, Rest>
@@ -125,15 +125,15 @@ export type GetFirstInequalityWhere<
125125
export type GetAllWhereConstraint<
126126
T extends MetaType,
127127
QCs extends QueryConstraints<T>[],
128-
WhereConstraintsAcc extends WhereConstraint<T, string, WhereFilterOp, unknown>
128+
WhereConstraintsAcc extends WhereConstraint<T, any, WhereFilterOp, unknown>
129129
> = QCs extends [infer H, ...infer R]
130130
? R extends QueryConstraints<T>[]
131131
?
132132
| WhereConstraintsAcc
133133
| GetAllWhereConstraint<
134134
T,
135135
R,
136-
| (H extends WhereConstraint<T, string, WhereFilterOp, unknown>
136+
| (H extends WhereConstraint<T, any, WhereFilterOp, unknown>
137137
? H
138138
: never)
139139
| WhereConstraintsAcc
@@ -152,7 +152,7 @@ type GetAllWhereConstraintOpStr<
152152
| GetAllWhereConstraintOpStr<
153153
T,
154154
R,
155-
| (H extends WhereConstraint<T, string, WhereFilterOp, unknown>
155+
| (H extends WhereConstraint<T, any, WhereFilterOp, unknown>
156156
? H['opStr']
157157
: never)
158158
| OpStrAcc
@@ -163,7 +163,7 @@ type GetAllWhereConstraintOpStr<
163163
export type WhereConstraintLimitation<
164164
T extends MetaType,
165165
Q extends GeneralQuery<T>,
166-
U extends WhereConstraint<T, string, WhereFilterOp, unknown>,
166+
U extends WhereConstraint<T, any, WhereFilterOp, unknown>,
167167
PreviousQCs extends QueryConstraints<T>[]
168168
> = ValidateWhereNotInArrayContainsAny<T, U, PreviousQCs> extends string
169169
? ValidateWhereNotInArrayContainsAny<T, U, PreviousQCs>

0 commit comments

Comments
 (0)