Skip to content

Commit

Permalink
Merge pull request #597 from supabase/fix/issue-1354-type-instanciati…
Browse files Browse the repository at this point in the history
…on-error

fix(types): type instantiation is excessively deep and possibly infinite
  • Loading branch information
avallete authored Jan 24, 2025
2 parents 7e985d4 + c2112d4 commit a0b56aa
Show file tree
Hide file tree
Showing 5 changed files with 368 additions and 21 deletions.
51 changes: 37 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@
"ts-expect": "^1.3.0",
"ts-jest": "^28.0.3",
"tsd": "^0.31.2",
"type-fest": "^4.32.0",
"typedoc": "^0.22.16",
"typescript": "4.5.5",
"typescript": "^4.5.5",
"wait-for-localhost-cli": "^3.0.0"
}
}
37 changes: 32 additions & 5 deletions src/PostgrestFilterBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import PostgrestTransformBuilder from './PostgrestTransformBuilder'
import { JsonPathToAccessor, JsonPathToType } from './select-query-parser/utils'
import { GenericSchema } from './types'

type FilterOperator =
Expand All @@ -25,6 +26,10 @@ type FilterOperator =
| 'phfts'
| 'wfts'

export type IsStringOperator<Path extends string> = Path extends `${string}->>${string}`
? true
: false

// Match relationship filters with `table.column` syntax and resolve underlying
// column value. If not matched, fallback to generic type.
// TODO: Validate the relationship itself ala select-query-parser. Currently we
Expand All @@ -40,6 +45,14 @@ type ResolveFilterValue<
: ResolveFilterRelationshipValue<Schema, RelationshipTable, Remainder>
: ColumnName extends keyof Row
? Row[ColumnName]
: // If the column selection is a jsonpath like `data->value` or `data->>value` we attempt to match
// the expected type with the parsed custom json type
IsStringOperator<ColumnName> extends true
? string
: JsonPathToType<Row, JsonPathToAccessor<ColumnName>> extends infer JsonPathValue
? JsonPathValue extends never
? never
: JsonPathValue
: never

type ResolveFilterRelationshipValue<
Expand Down Expand Up @@ -75,7 +88,12 @@ export default class PostgrestFilterBuilder<
column: ColumnName,
value: ResolveFilterValue<Schema, Row, ColumnName> extends never
? NonNullable<unknown>
: NonNullable<ResolveFilterValue<Schema, Row, ColumnName>>
: // We want to infer the type before wrapping it into a `NonNullable` to avoid too deep
// type resolution error
ResolveFilterValue<Schema, Row, ColumnName> extends infer ResolvedFilterValue
? NonNullable<ResolvedFilterValue>
: // We should never enter this case as all the branches are covered above
never
): this {
this.url.searchParams.append(column, `eq.${value}`)
return this
Expand All @@ -91,7 +109,9 @@ export default class PostgrestFilterBuilder<
column: ColumnName,
value: ResolveFilterValue<Schema, Row, ColumnName> extends never
? unknown
: ResolveFilterValue<Schema, Row, ColumnName>
: ResolveFilterValue<Schema, Row, ColumnName> extends infer ResolvedFilterValue
? ResolvedFilterValue
: never
): this {
this.url.searchParams.append(column, `neq.${value}`)
return this
Expand Down Expand Up @@ -269,9 +289,16 @@ export default class PostgrestFilterBuilder<
*/
in<ColumnName extends string>(
column: ColumnName,
values: ResolveFilterValue<Schema, Row, ColumnName> extends never
? unknown[]
: ReadonlyArray<ResolveFilterValue<Schema, Row, ColumnName>>
values: ReadonlyArray<
ResolveFilterValue<Schema, Row, ColumnName> extends never
? unknown
: // We want to infer the type before wrapping it into a `NonNullable` to avoid too deep
// type resolution error
ResolveFilterValue<Schema, Row, ColumnName> extends infer ResolvedFilterValue
? ResolvedFilterValue
: // We should never enter this case as all the branches are covered above
never
>
): this {
const cleanedValues = Array.from(new Set(values))
.map((s) => {
Expand Down
Loading

0 comments on commit a0b56aa

Please sign in to comment.