pocketbase/js-sdk

Better typescript support

datner opened this issue · 3 comments

datner commented

The current method is casting the Client to some extended interface that matches it

interface TypedPocketBase extends PocketBase {
  collection(idOrName: string): RecordService // default fallback for any other collection
  collection(idOrName: 'tasks'): RecordService<Task>
  collection(idOrName: 'posts'): RecordService<Post>
}

...

const pb = new PocketBase("http://127.0.0.1:8090") as TypedPocketBase;

This is ok in the sense that it works, but it's inelegant..
Client is currently the default export and is not exported in general. The conversation regarding the pitfalls of default exports aside, if Client is also just plainly exported it could then be overloaded in a declaration file, which will allow (just allow! not blocking the current solution that will stay the exact same!!) the following

declare module "pocketbase" {
  interface Client  {
    collection(idOrName: 'tasks'): RecordService<Task>
    collection(idOrName: 'posts'): RecordService<Post>
  }
}

Making the client typed "by default" instead of needing to cast it. While Client is only default exported, this is impossible to do without workarounds.

the only change required is changing

export { Client as default }

to

export { Client, Client as default }

so there are no breaking changes

I'm sorry, this seems confusing to me.

If you think that it can be done better, feel free to open a PR and I'll try to review it, but for now I'm not planning making changes to TS declarations or the default export.

Option to autogenerate a typed client eventually will be handled as part of #152.

datner commented

@ganigeorgiev as I was trying to stress, no changes are required to neither. The only change required by the library is adding Client to also be a regular named export. This has nothing to do with a generated typed client though that would be lovely regardless. I'll open a PR

datner commented

Opened a PR