Closed isaacharrisholt closed 8 months ago
Thanks @isaacharrisholt! There's little reason to use interface
now that I think about it, so unless this breaks existing projects this change LGTM.
It can break if:
interface ExtendedDatabase extends Database {}
interface Database { custom_schema: {} }
So in theory it is a breaking change, but at the same time, it's: a) generated code b) very low change of happening
In this case I think it's worth the risk - we can reconsider if we get complaints about it.
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...
...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
.