如何将已声明的条件推断类型与另一个类似的条件推断类型进行匹配

时间:2020-01-17 09:25:15

标签: typescript typescript-typings indexeddb typescript-generics

the previous issue开始。我现在正在基于IDB定义API接口。条件推断类型与KnownKeys类型冲突,这使我无法集成IDB声明。
这是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.
}

我不知道如何匹配StoreNames<Schema>的要求(由KnownKeys实现)。即使在S extends StoreNames<Schema>中使用DBStoreInfer也无法解决。
目前,我直接使用IDB类型来逛逛

...
  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.
...

有什么办法可以解决?还是有重新设计的想法?

更新:

我提出了一个用例。它建立在base interface(正在开发)

// 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()

更新2

Another definition(针对Jsonp / Ajax)。进行设计讨论。

0 个答案:

没有答案