有人可以帮我理解这里发生了什么吗:TS 播放器
基本上我有一个具有 exec
方法的 store
,我想要缩小子进程中 exec
参数的类型。但是似乎存储类型是一种通用类型,存在错误。
type Param<Options> = {
[K in keyof Options]: Readonly<{
id: K,
options: Options[K],
}>
}[keyof Options];
interface Store<Options> {
exec: (nextState: Param<Options>) => void
}
type ParentOptions = {
'a': { a: string },
} & SubOptions
type SubOptions = {
'b': { b: number },
}
function test(
parentFlowExec: (nextState: Param<ParentOptions>) => void,
subFlowExec: (nextState: Param<SubOptions>) => void,
parentNonGeneric: { exec: (nextState: Param<ParentOptions>) => void },
subNonGeneric: { exec: (nextState: Param<SubOptions>) => void },
parentFlow: Store<ParentOptions>,
subFlow: Store<SubOptions>,
) {
parentFlowExec = subFlowExec; // error: ok
subFlowExec = parentFlowExec; // passed
parentNonGeneric = subNonGeneric; // error: ok
subNonGeneric = parentNonGeneric; // passed
parentFlow = subFlow; // error: ok
subFlow = parentFlow; // error ??
// I plan to use it like this
subProcess(parentFlow);
}
function subProcess(flowStore: Store<SubOptions>) {
flowStore.exec({ id: 'a', options: { a: 'a' } }); // can't call with 'a'
flowStore.exec({ id: 'b', options: { b: 3 } }); // ok
}
更新: 我把 Param 移出来并且已经it working,但仍然不明白为什么它们的嵌套不起作用。
interface Store<Options> {
exec: (nextState: Options) => void
}
// parent2: Store2<Param<ParentOptions>>,
// sub2: Store2<Param<SubOptions>>,
subFlow
和parentFlow
彼此不变。请参见我的问题:https://dev59.com/OVEG5IYBdhLWcg3wac6f - captain-yossarian from Ukraine