如何匹配联合类型中的不同键
How to match different keys inside a union type
我不确定如何提出这个问题...但希望代码示例能够阐明我的问题:
type A = {| +type: "A", +payload: "PA" |};
type B = {| +type: "B", +payload: "PB" |};
type Actions = A | B;
function dispatch<T: Actions>(type: $PropertyType<T, 'type'>, payload: $PropertyType<T, 'payload'>) {
}
dispatch("A", "PA"); // ok
dispatch("B", "PB"); // ok
dispatch("A", "PB"); // this doesn't error, but I'd like to
dispatch("B", "PA"); // this doesn't error, but I'd like to
dispatch("C", "PC"); // this errors properly
您需要一个允许根据键查找类型的类型,不幸的是,我认为标记联合不允许这样做。例如,您可以将函数更改为:
type ActionsLookup = {
A: A,
B: B,
};
type Actions = $Values<ActionsLookup>;
function dispatch<K: $Keys<ActionsLookup>>(
type: K,
payload: $ElementType<$ElementType<ActionsLookup, K>, 'payload'>
) {}
我不确定如何提出这个问题...但希望代码示例能够阐明我的问题:
type A = {| +type: "A", +payload: "PA" |};
type B = {| +type: "B", +payload: "PB" |};
type Actions = A | B;
function dispatch<T: Actions>(type: $PropertyType<T, 'type'>, payload: $PropertyType<T, 'payload'>) {
}
dispatch("A", "PA"); // ok
dispatch("B", "PB"); // ok
dispatch("A", "PB"); // this doesn't error, but I'd like to
dispatch("B", "PA"); // this doesn't error, but I'd like to
dispatch("C", "PC"); // this errors properly
您需要一个允许根据键查找类型的类型,不幸的是,我认为标记联合不允许这样做。例如,您可以将函数更改为:
type ActionsLookup = {
A: A,
B: B,
};
type Actions = $Values<ActionsLookup>;
function dispatch<K: $Keys<ActionsLookup>>(
type: K,
payload: $ElementType<$ElementType<ActionsLookup, K>, 'payload'>
) {}