I have simplified code snippet:
interface Store<T> {
value: T
}
type AnyStore = Store<any>
type StoreValue<T> = T extends Store<infer V> ? V : never
function computed<
V,
D extends AnyStore,
S extends Store<V>
>(
createStore: (value: V) => S,
depStore: D,
compute: (value: StoreValue<D>) => V
): S {
return createStore(compute(depStore.value))
}
interface MetaStore<T> extends Store<T> {
meta: {}
}
function meta<T>(value: T): MetaStore<T> {
return { value, meta: {} }
}
const depStore: Store<number> = { value: 404 }
const computedStore = computed(meta, depStore, num => `${num}`)
Playground
So I got problems with computedStore
variable type
const computedStore = computed(meta, depStore, num => `${num}`)
// const computedStore: MetaStore<unknown>
// but should MetaStore<string>
If callback’s argument type will be defined explicitly, then computedStore
variable type is correct
const computedStore = computed(meta, depStore, (num: number) => `${num}`)
// const computedStore: MetaStore<string>
but I want to have working inference here for best DX. How to achieve desired result?