Skip to content

Commit a0b56aa

Browse files
authored
Merge pull request #597 from supabase/fix/issue-1354-type-instanciation-error
fix(types): type instantiation is excessively deep and possibly infinite
2 parents 7e985d4 + c2112d4 commit a0b56aa

File tree

5 files changed

+368
-21
lines changed

5 files changed

+368
-21
lines changed

package-lock.json

+37-14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@
5959
"ts-expect": "^1.3.0",
6060
"ts-jest": "^28.0.3",
6161
"tsd": "^0.31.2",
62+
"type-fest": "^4.32.0",
6263
"typedoc": "^0.22.16",
63-
"typescript": "4.5.5",
64+
"typescript": "^4.5.5",
6465
"wait-for-localhost-cli": "^3.0.0"
6566
}
6667
}

src/PostgrestFilterBuilder.ts

+32-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import PostgrestTransformBuilder from './PostgrestTransformBuilder'
2+
import { JsonPathToAccessor, JsonPathToType } from './select-query-parser/utils'
23
import { GenericSchema } from './types'
34

45
type FilterOperator =
@@ -25,6 +26,10 @@ type FilterOperator =
2526
| 'phfts'
2627
| 'wfts'
2728

29+
export type IsStringOperator<Path extends string> = Path extends `${string}->>${string}`
30+
? true
31+
: false
32+
2833
// Match relationship filters with `table.column` syntax and resolve underlying
2934
// column value. If not matched, fallback to generic type.
3035
// TODO: Validate the relationship itself ala select-query-parser. Currently we
@@ -40,6 +45,14 @@ type ResolveFilterValue<
4045
: ResolveFilterRelationshipValue<Schema, RelationshipTable, Remainder>
4146
: ColumnName extends keyof Row
4247
? Row[ColumnName]
48+
: // If the column selection is a jsonpath like `data->value` or `data->>value` we attempt to match
49+
// the expected type with the parsed custom json type
50+
IsStringOperator<ColumnName> extends true
51+
? string
52+
: JsonPathToType<Row, JsonPathToAccessor<ColumnName>> extends infer JsonPathValue
53+
? JsonPathValue extends never
54+
? never
55+
: JsonPathValue
4356
: never
4457

4558
type ResolveFilterRelationshipValue<
@@ -75,7 +88,12 @@ export default class PostgrestFilterBuilder<
7588
column: ColumnName,
7689
value: ResolveFilterValue<Schema, Row, ColumnName> extends never
7790
? NonNullable<unknown>
78-
: NonNullable<ResolveFilterValue<Schema, Row, ColumnName>>
91+
: // We want to infer the type before wrapping it into a `NonNullable` to avoid too deep
92+
// type resolution error
93+
ResolveFilterValue<Schema, Row, ColumnName> extends infer ResolvedFilterValue
94+
? NonNullable<ResolvedFilterValue>
95+
: // We should never enter this case as all the branches are covered above
96+
never
7997
): this {
8098
this.url.searchParams.append(column, `eq.${value}`)
8199
return this
@@ -91,7 +109,9 @@ export default class PostgrestFilterBuilder<
91109
column: ColumnName,
92110
value: ResolveFilterValue<Schema, Row, ColumnName> extends never
93111
? unknown
94-
: ResolveFilterValue<Schema, Row, ColumnName>
112+
: ResolveFilterValue<Schema, Row, ColumnName> extends infer ResolvedFilterValue
113+
? ResolvedFilterValue
114+
: never
95115
): this {
96116
this.url.searchParams.append(column, `neq.${value}`)
97117
return this
@@ -269,9 +289,16 @@ export default class PostgrestFilterBuilder<
269289
*/
270290
in<ColumnName extends string>(
271291
column: ColumnName,
272-
values: ResolveFilterValue<Schema, Row, ColumnName> extends never
273-
? unknown[]
274-
: ReadonlyArray<ResolveFilterValue<Schema, Row, ColumnName>>
292+
values: ReadonlyArray<
293+
ResolveFilterValue<Schema, Row, ColumnName> extends never
294+
? unknown
295+
: // We want to infer the type before wrapping it into a `NonNullable` to avoid too deep
296+
// type resolution error
297+
ResolveFilterValue<Schema, Row, ColumnName> extends infer ResolvedFilterValue
298+
? ResolvedFilterValue
299+
: // We should never enter this case as all the branches are covered above
300+
never
301+
>
275302
): this {
276303
const cleanedValues = Array.from(new Set(values))
277304
.map((s) => {

0 commit comments

Comments
 (0)