简体   繁体   English

如何转换“|” 至 ”&”

[英]How to transform “|” to “&”

I use rematch and typescript in my project.我在我的项目中使用rematchtypescript I want to get all effects function type from a models,like this.我想从模型中获得所有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;
}

 */

Result is model1 | model2结果是模型model1 | model2 model1 | model2 . model1 | model2 What I want is model1 & model2 .我想要的是model1 & model2

How to write the Transform type如何编写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;

This may or not be what you want to do but if you want to switch out the |这可能是也可能不是您想要做的,但如果您想切换 | with & then think about using regex.与 & 然后考虑使用正则表达式。

You don't necessarily need to use a transform function for this.您不一定需要为此使用变换 function。

this is a possible solution to your question to transform |这是您转换问题的可能解决方案| to &至 &

with regex you can use the replace functionality ()=().replace("\|","&");使用正则表达式,您可以使用替换功能 ()=().replace("\|","&");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM