CLI type generation and UUIDs

When I run npx supabase gen types typescript > database.types.ts, all my primary and foreign keys that are UUIDs in SQL become string in TypeScript. Is there a way to make this a TS UUID type? (Other than redefining all columns in my TS app — not really an option given the amount of foreign keys in use). Or are there good reasons to not want this? I guess the query itself also results in type string? Do we know if there is a specific reason for that? The reason I ask is that the following does not give a type error, but it does give a PostgREST error:
createClient()
.from('users')
.select('*')
.eq('id', '') // ❌ gives a 400 error
createClient()
.from('users')
.select('*')
.eq('id', '') // ❌ gives a 400 error
So when I accidentally set a default value to emtpy string rather than null, my app began firing requests and throwing errors.
2 Replies
silentworks
silentworks2mo ago
When you manually changed it to uuid type did it warn you about the type mismatch error with the query above? I'm not sure why Supabase uses string but I don't think there is a native UUID type in Typescript so it's always a string.
Jacob | Landscape AI
Well, I did find that there is a UUID type in the crypto module. But I get that this is rather non-standard. I'll stick with keeping it internally consistent with supabase-js, which just delivers these variables as strings. 🙂 And I did get a type error for the encapsulating function when I required the userId input to be UUID. Then it complained that string was not of type ${string}-${string}-${string}-${string}-${string} (the definition from crypto). But now it also complains when I do pass in a userId i got directly from the database (which is a string type). Thanks for your help! 😊

Did you find this page helpful?