TypeScript reduces runtime errors and makes code more maintainable. In this lesson, we use the Supabase CLI to introspect our PostgreSQL schema and generate TypeScript definitions.
Additionally, we resolve a collection of TypeScript errors and create a custom union type for our transformed tweets - making it globally available throughout our Next.js application.
Custom type for TweetWithAuthor
type TweetWithAuthor = Tweet & {
author: Profile;
likes: number;
user_has_liked_tweet: boolean;
};
Generate TypeScript definitions
npx supabase gen types typescript --project-id your-project-id > lib/database.types.ts
Correction: this is an intersection
type, not a union
! 🧠
https://www.typescriptlang.org/docs/handbook/2/objects.html#intersection-types
small suggestion: Supabase provides a helper type reference table row types: Tables<"tweets">
instead of DB["public"]["tables"]["tweets"]...