【发布时间】:2021-03-02 09:16:38
【问题描述】:
我有一个Actions 类型,需要映射到actionMap 变量中的一组回调。
但是,我不确定如何强制Action 类型的第一个通用元素的类型。显然key in keyof Actions 没有在这里做事。我需要改用什么?
export interface Action<T, U> {
type: T,
data: U,
}
export type Actions =
| Action<"something", {}>
| Action<"somethingElse", {}>
const actionMap: { [key in keyof Actions]: (action: Actions) => any } = {
// I'd like the value of the `type` property of all my actions enforced here. Ex:
// "something": (action) => {}
// "somethingElse": (action) => {}
}
如果我问错了问题,什么是更好的问题?我不确定我是否使用了相应的行话。
【问题讨论】:
标签: typescript generics types keyof typeguards