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.