Open okomarov opened 1 year ago
I had the same issue. As lame as this is, I ended up just using the as
operator to cast the DB when calling the create*
methods in the root db upgrade function like so:
const main = async () => {
const client = await openDB<Schema>('myDb', 1, {
upgrade(db) {
createPortfolio(db as unknown as PortfolioSchema);
createUser(db as unknown as UserSchema);
},
});
};
Very much a hacky workaround but I fiddled for a while with typings to make it work and couldn't find any trick to it. Given that the DBSchema
uses index types I suspect it isn't possible, but would be interested if any Typescript experts know of any way.
I'm not sure about the type guard issue.
This simplified example can be found on Replit.
I want to organise my file structure by store:
In
index.ts
I have:where the first issue with e.g.
createPortfolio(db)
:which is of the type
A | B is not assignable to A
. So, I want to narrow downdb
, where the definition of the sub-schema inportfolio.ts
and the type guard are (theuser.ts
is very similar):The issue with the type guard is:
How can I achieve sub-schema composition where interfaces, sub-schemas and methods are all defined in separate files?