0
votes

The library code I have lets the user define a set of tables with the keys and types. In the callback function a record function is given. With this they can supply a data object (defaults to first item in the array of definitions) or table name & data object.

A definition looks like:

    export enum DATABASE_TYPES { // Can be changed to a Object literal with `as const` if needed
      "INTEGER" = "INTEGER", //->number
      "NULL" = "NULL",//->null
      "TEXT" = "TEXT"//->string
    };

    export type DBDataTypes = { [key: string]: DATABASE_TYPES };
    export type DBDataTypesConfig = { name: string; definition: DBDataTypes }[]; 
    type DBUserDef = DBDataTypes | DBDataTypesConfig; //<-- type that the user passes in, if DBDataTypes passed in, then will be converted to DBDataTypesConfig with the name of "log"

What I am hoping for is some type safety, so that only the names defined in the definitions are allowed, and as a bonus the conversion of the DATABASE_TYPES to the TS counter parts.

Now I may be asking a lot of the TS compiler, and it might not be possible, but if anyone has any ideas, more than happy to listen. This is going to be a breaking change release anyway, so can change some of the signatures if needed.

TS Playground: https://tsplay.dev/mbB58m