【发布时间】:2021-04-02 18:11:45
【问题描述】:
我在我的项目中使用rematch 和typescript。
我想从模型中获取所有 effects function 类型,就像这样。
const models = {
model1: {
effects: {
fn1: () => {},
fn2: () => {},
},
},
model2: {
effects: {
fn3: () => {},
fn4: () => {},
},
},
};
type Result = {
fn1?: () => void;
fn2?: () => void;
fn3?: () => void;
fn4?: () => void;
};
type Models = typeof models;
type EffectsType = { [key in keyof Models]: Models[key]['effects'] }[keyof Models];
/*
this is result type
type EffectsType = {
fn1: () => void;
fn2: () => void;
} | {
fn3: () => void;
fn4: () => void;
}
*/
结果是model1 | model2。我要的是model1 & model2。
Transform 类型的写法
// how to write this type
type Transform<T extends object> = any;
type obj1 = {
a: string;
b: string;
};
type obj2 = {
b: string;
c: string;
};
type from = obj1 & obj2;
type result = Transform<from>;
// this is result
type result = obj1 | obj2;
【问题讨论】:
标签: typescript redux types