[英]How to transform “|” to “&”
我在我的项目中使用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
。 我想要的是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;
这可能是也可能不是您想要做的,但如果您想切换 | 与 & 然后考虑使用正则表达式。
您不一定需要为此使用变换 function。
这是您转换问题的可能解决方案| 至 &
使用正则表达式,您可以使用替换功能 ()=().replace("\|","&");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.