-
Notifications
You must be signed in to change notification settings - Fork 364
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support discriminated union type or similar in database schemas #275
Comments
Or even something like the following, where the corresponding type for every key is indexable, with the top-level entries of said type being used as the key/value pairs. Contrived example: interface DatabaseSchema extends DBSchema {
posts: Record<string, Post>; // Like `list: { key: string; value: Post }`
metadata: {
times: { joined: string, lastSeen: string };
counts: { posts: number; privateMessages: number };
};
} |
This seems doable. A separate generic parameter would need to be introduced for the key. That key can then be used to narrow the type of the value. Here's an updated version of the export type StoreValue<
DBTypes extends DBSchema | unknown,
StoreName extends StoreNames<DBTypes>,
Key = any
> = DBTypes extends DBSchema
? (DBTypes[StoreName] & (Key extends StoreKey<DBTypes, StoreName> ? { key: Key } : {}))['value']
: any;
get<
Name extends StoreNames<DBTypes>,
Key extends StoreKey<DBTypes, Name> | IDBKeyRange
>(
storeName: Name,
query: Key
): Promise<StoreValue<DBTypes, Name, Key> | undefined>; This works on the initial example. When an |
Currently, there is no way to strongly type a keyval store that has only certain keys (each with its own value type).
For example:
What I would like is some way for a discriminated union (based on key type, obviously) to be detected and used.
The
key
passed to database functions would then be used to provide better typing.One of my other ideas for what this could look like, instead of a discriminated union:
The text was updated successfully, but these errors were encountered: