0

Continue from the previous issue. I am now defining an API interface based on IDB. An conditional infer type conflicts with the KnownKeys type, which stops me from integrating declaration of IDB.
Here is the details.

interface Provider<
  Schema extends DBSchema = any,
  Mapper extends Record<string, Invoking<Schema>> = {}
  > {
  /**
   * Type 'Store' does not satisfy the constraint 'StoreNames<Schema>'.
   * Type 'DBStoreInfer<Schema, Mapper[Name]>' is not assignable to type 'StoreNames<Schema>'.(2344)
   */
  register<
    Name extends keyof Mapper,
    Store extends DBStoreInfer<Schema, Mapper[Name]>
  >(api: Api<Name, Schema, Store>): any // Error.
}

I dont know how to match the requirement of StoreNames<Schema>(implemented by KnownKeys). Even using S extends StoreNames<Schema> in DBStoreInfer can not resolve it.
Currently, I walk around by using the IDB type directly

...
  register<
    Name extends keyof Mapper,
    Store extends StoreNames<Schema>
  >(api: Api<Name, Schema, Store>): any // Good. But lost type validation in other class using Mapper.
...

Any way to fix it? Or any idea to rework the design?

Update:

I come up with a use case. It is built on a base interface(developing)

// Model
type Entity = {
  id: string,
  code: string,
  data: number
}

// Somewhere in the common module.
declare const provider: DBProviderType<{
  'db-store': {
    key: string,
    value: Entity,
    indexes: {
      'db-compound-index': [string, number],
      'db-code-index': string
    }
  }
}, {
  testApi: (def: {
      store: 'db-store',
      method: 'get'
      indexes: ['code'],
      query: Entity
    }
  ) => Entity
}>

// Somewhere during app init. The Mapper type may be a subset.
// Arguments follow the structure of Mapper and Schema
provider.registerApi({
  name: 'testApi',
  store: 'db-store',
  method: 'get',
  indexes: 'db-compound-index'
})

// Somewhere in business logic
provider.invokeApi('testApi')
  .indexes('db-code-index')
  .query({
    code: 'test-code',
    data: 1001
  })
  .invoke()

Update 2

Another definition for Jsonp/Ajax. For design discussion.

Community
  • 1
  • 1
Luciens
  • 338
  • 1
  • 11
  • 1
    You went hard here :D – Maciej Sikora Jan 17 '20 at 10:08
  • @MaciejSikora I try my best. – Luciens Jan 17 '20 at 10:13
  • Can you write what you want to achieve in terms of use case? I feel its overcomplicated – Maciej Sikora Jan 17 '20 at 10:40
  • @MaciejSikora Yes I also feel it overcomplicated. I update a use case. What I want is built a type-safed DB module. When using it, define a subset mapper as API type for end user. – Luciens Jan 17 '20 at 12:00
  • have you considered just using templated functions and being done with it? in other words, typify at a higher api layer rather than trying to get idb itself typed, and do all data access via the proxy layer where functions are templated – Josh Jan 18 '20 at 05:42
  • @Josh I like template function. What I dont know is how to make the type check work for proxy layer when the inner adapter changed. I update another interface for Jsonp/fetch. I am trying to simplify it. – Luciens Jan 18 '20 at 07:07

0 Answers0