Skip to content

Support discriminated union type or similar in database schemas #275

@darrylnoakes

Description

@darrylnoakes

Currently, there is no way to strongly type a keyval store that has only certain keys (each with its own value type).

For example:

interface DatabaseSchema extends DBSchema {
  metadata:
    | {
        key: "foo";
        value: { bar: number };
      }
    | {
        key: "baz";
        value: string;
      };
}

...

const foo = await db.get("metadata", "foo");
// Type of `foo`: string | { bar: number } | undefined

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:

interface Metadata extends DBSchemaStore = {
  foo: { bar: number };
  baz: string;
};

interface DatabaseSchema extends DBSchema {
  metadata: Metadata;
}

const foo = await db.get("metadata", "foo");
// Type of `foo`: { bar: number } | undefined

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions