change(typegen: TS): Export type, not interface #689
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What kind of change does this PR introduce?
Minor change
What is the current behavior?
Currently, the TypeScript type generation exports the
Database
type as aninterface
. However, this is somewhat problematic in that TypeScriptinterface
s don't implicitly define an index signature for their keys.What do I mean by this?
Consider the following example:
I would expect to be able to call
getPokemon
withMyPokedex
as the type parameter. However, this results in a type error:What's the problem?
This means that if a library author wants to create a generic function where the expected type parameter is the user's
Database
type, there's no way for them to create a constraint.For example, isaacharrisholt/supawright uses the user's
Database
to accurately type input and output types for some of its functions (much likesupabase-js
). It uses the following code to constrain the type parameter passed into thewithSupawright
function:(yes, I'm stealing from Supabase, but it works)
However, if I try to have
withSupawright<Database extends GenericDatabase>()
and then call it with the exportedDatabase
interface, I get the error mentioned above:Changing the exported
interface
to instead be atype
resolves this.We've been running the following...
types: pnpm supabase gen types typescript --local | \ sed 's/export interface Database {/export type Database = {/' \ > src/types/database.ts
...in production for a while now, and it has no negative impact on the Supabase client, and doing
SupabaseClient<Database>
still works as expected.It would make Supabase extension libraries like this one much easier to write, and I can't think of any downsides to this change.
I might be missing something huge, in which case, let me know.
What is the new behavior?
The TypeScript type generator returns a
type
, not aninterface
.