【发布时间】:2021-10-02 00:37:56
【问题描述】:
我的应用中有以下功能:
const store = createStore(
{ value: false },
(set, get) => ({
changeValue: (newValue: boolean) => {
set((state) => { state.value = newValue })
}
})
)
我正在尝试获取它,因此生成的 store 的类型是:
{ value: boolean, changeValue: (newValue: boolean) => void }
无需手动添加类型。我觉得应该有一种方法可以从该函数中 createStore 的 args 推断类型,但我无法弄清楚。
这是我的createStore 目前的样子:
export const createStore = <Document extends Record<string, unknown>>(
document: Document,
actions: () => Record<string, unknown>,
) => {
return Object.assign({}, document, actions())
}
const test = createStuff({ hello: 'world' }, () => ({ update: () => 'test' }))
在这种情况下,test 的类型是
{
hello: string;
} & Record<string, unknown>
我希望它是:
{
hello: string;
} & { update: () => string }
【问题讨论】:
-
这是来自 redux 的
createStore吗?还是这是您自己创造的? -
这是定制的,我很确定内部不相关,但我想我可能错了。
-
我的意思是你在谈论推断函数的返回类型`createStore
, but you haven't shared that function with us, or what it actually returns. We need to seecreateStore` 以便能够提供帮助。 -
好的,我添加了上面 createStore 的示例
标签: typescript