[英]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.