supabase-js: Json type: Type instantiation is excessively deep and possibly infinite.
Bug report
- I confirm this is a bug with Supabase, not with my own application.
- I confirm I have searched the Docs, GitHub Discussions, and Discord.
Describe the bug
I have a Vue/Nuxt project with TypeScript which is throwing me an error with the profile table when I try to assign it to a variable based on the types generated from the DB. “Type instantiation is excessively deep and possibly infinite.”
The problematic column in question is one called “links” which stores JSONB data. The type that is generated from the Supabase Cli using the command npx supabase gen types typescript creates a self referencing type called Json.
export type Json =
| string
| number
| boolean
| null
| { [key: string]: Json | undefined }
| Json[]
With my (generated) profile row types looking like:
profile: {
Row: {
about: string | null
available: string | null
avatar: string | null
avatar_url: string | null
created_at: string | null
id: number
links: Json | null // this is the problematic column.
location: string | null
name: string | null
poster: string | null
poster_url: string | null
tagline: string | null
updated_at: string
user_id: string
username: string | null
}
}
In my Pinia Vue store I get the following error when trying to assign it against these types.
import type { Database } from '@/types/supabase'
import { defineStore } from 'pinia'
export const useUserStore = defineStore('user', {
state: () => ({
profile: null as Database['public']['Tables']['profile']['Row'] | null
}),
actions: {
setProfile(profile: Database['public']['Tables']['profile']['Row'] | null) {
this.profile = profile // Here is where the TS error occurs.
}
}
})
I’m guessing this error happens because the generated type is self referencing and TS does not know how deep it would be?
To Reproduce
Steps to reproduce the behavior, please provide code snippets or a repository:
- Create a column and set its type to jsonb
- Generate types using the Supabase Cli
- Try to do something similar and see that TS complains.
Expected behavior
There should be no TS error.
Screenshots
System information
- OS: Apple Macbook Pro M2 Ventura 13.4
- Version of supabase-js: 2.26.0
- Version of Node.js: 18.16.0
Additional context
I have not been able to see similar issues with other Supabase users but perhaps the use of JSONB is quite rare?
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 34
- Comments: 24 (3 by maintainers)
@kiwicopple, this is such a simple thing to fix, yet it kills the developer experience (through extensive CPU usage / slow VSC) of anyone working on any half-serious project with Supabase and Typescript. Fixing these small issues has a much bigger impact to existing customers than anything you release during launch weeks, yet these never get prioritised, even (or especially if) the PRs are created by the community. Been a customer for 3+ years, and this issue is soon clocking in on 2 years, potentially longer. Very disappointing.
Using this StackOverflow post as an example, I have modified it for the
Jsontype:Ddefines the depth limit. When the depth limit is reached (D extends DA['length']),anyis used to stop the recursion. Might require some testing but it works for me and retains type safety.I did a little more digging and I think it may be an issue in combination with Vue’s reactivity and not directly Supabase afterall. You may see something similar with React?
If I try to a assign to a simple variable with
letfor example it’s okay but usingref()in Vue causes the warning, possibly due to Vue / TS not knowing how deep it would have to watch the reactive object.If I use
shallowRef()instead, it’s fine: https://vuejs.org/api/reactivity-advanced.html#shallowrefBump
I gotta say I love supabase a ton. Such an amazing platform with fantastic UI/UX and so many great features. This Json typing issue has been the number 2 biggest hurdle for me in working with the supabase libraries. Number 1 was definitely database transactions (plpgsql ftl).
I remember seeing a PR up a while ago to swap the Json type for an
unknownin the generated types. That would be quick and easy fix for this, and it could be behind a flag in the types generation script. Can’t find the PR anymore though… I’m just trying to avoid manually updating my generated types every time I re-generate them via cli.Are there any plans on the supabase side to build Json field typing on top of Postgres? Like a Json column could have an expected schema which could be defined in the UI. This could then be used for types generation. Perhaps schema validation if you wanted to get fancy with it, but I assume that’s crossing into a territory the platform may not want to go toward.
Fix I’m using is to update your supabase generated types to have
Jsonas below:The Json type does reference itself. Could it be that? I’ve also been having the same problem with the Json type columns.
Edit: changed my generated Json type to this and it all went away:
The error is complaining in my opinion that the line
| { [key: string]: Json | undefined }has no end to type instantiation, which when you think about it, is true.Its probably that loop of autoreferencing without finishing the main json reference.
@encima
I would suggest the following solution, since I implemented this, I’ve never had a problem:
This is still going on.
I made something for this before maybe someone wants to use it. When generating types with supabase, it replaces the json type with your custom type
app/types/generate-types.js:
and on package.json:
Also for validation on Supabase side, you can use
pg_jsonschema: https://supabase.com/docs/guides/database/extensions/pg_jsonschemaI’m still dying
Just wanted to confirm this is still an issue at time of writing with the latest version of Vue and supabase-js and makes it impossible to work with supabase-js in any serious capacity when using Typescript and Vue.
Any status on this? :I
I have add this issue in various table but the solution for us has been to “patch” the generated type using type-fest, and only use this type in the rest of our app:
It’s a bit of manual work but it worked well for us. We wanted to make this automatic by detecting the usage of
Jsoninside the schema and replacing it with one of the version suggested above in this thread by using something similar to the schema offered by type-fest and we came up with the following solution (take this with a grain of salt, this is experimental):Bringing the comments from the related issue made for the CLI here.
Summary of suggestions:
Jsontypes asunknownand allow the developer to decideJsontype asRecordwith astringkey andanyvalueMergeDeepfrom the typefest libraryJsontype toany