-
-
Notifications
You must be signed in to change notification settings - Fork 146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(types): type instantiation is excessively deep and possibly infinite #597
base: master
Are you sure you want to change the base?
Conversation
When using MergeDeep type-fest even with simple structures we get this error For typescript > 4.5.5 this trick fix the issue by forcing TS to infer the value And reuse it instead of computing multiple time
src/PostgrestFilterBuilder.ts
Outdated
value: ResolveFilterValue<Schema, Row, ColumnName> extends infer ResolvedFilterValue | ||
? ResolvedFilterValue extends never | ||
? NonNullable<unknown> | ||
: NonNullable<ResolvedFilterValue> | ||
: never |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The actual tricks is to use the infer
and never
to force typescript to infer the right type and not stop at "possibly infinite recursive".
values: ReadonlyArray< | ||
ResolveFilterValue<Schema, Row, ColumnName> extends infer ResolvedFilterValue | ||
? ResolvedFilterValue extends never | ||
? unknown[] | ||
: ResolvedFilterValue | ||
: never | ||
> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here we need the infer
within the ReadonlyArray
to have the proper resulting type, otherwise we'll have an invalid union.
// @ts-expect-error should not be able to eq 'id' since the column does not | ||
// exist | ||
.eq('id', 2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this test should never had passed because the query actually is made to return an error. And id
column doesn't exist in users
table.
We can make it pass tough by changing the never
into an unknown
here:
Not All Heroes Wear Capes 🫡 |
What kind of change does this PR introduce?
Bug fix a case on typescript > 4.5.5 where using
MergeDeep
override lead to an error ofType instantiation is excessively deep and possibly infinite
. Use a trick to "force typescript to infer final type" instead of doing a shallow inspection and warning about possible infinite recursion.This is a bit black magic'ish but it fix the issue on the reproduction case 😅
Additional context
Related: supabase/supabase-js#1354