TypeScript - 基于鉴别器将联合类型映射到另一个联合类型的函数

时间:2018-05-12 02:33:44

标签: typescript discriminated-union

我有2个使用相同鉴别器字段+值的区别联合类型。我正在尝试编写一个可以根据鉴别器将1映射到另一个的函数。

e.g。

输入类型:

type InA = {
    type: 'a',
    data: string
};

type InB = {
    type: 'b',
    data: number
};

type In = InA | InB;

输出类型:

type OutA = {
    type: 'a',
    data: Object
};

type OutB = {
    type: 'b',
    data: Array<number>
};

type Out = OutA | OutB;

制图功能

// This is the function I'd like to have a better type signature
// for inferring output type based on input type
function map<In, Out>(
    in: In
): Out {
   // do something
}

用法

// I want the compiler to infer that this is OutB based on the InB
let result = map({ type: 'b', value: 999 });

有没有办法为地图编写功能签名,以便这样做?

在收到答案后更新 我能够使用@ titian-cernicova-dragomir答案的修改版本。这是关于我如何使用它的一般想法+一些额外的背景:

/** Http Request types **/

type RequestA = {
    type: 'names',
    url: '/names'
};

type RequestB = {
    type: 'numbers',
    url: '/numbers'
};

type Request = RequestA | RequestB;

/** Response types **/

type ResponseA = {
    type: 'names',
    data: Array<string>
};

type ResponseB = {
    type: 'numbers',
    data: Array<number>
};

type Response = ResponseA | ResponseB;

/** Helper from accepted answer */
type GetOut<T, A> = T extends { type: A } ? T : never;

/** Generic function for fetching data */
export function fetchData<
    Req extends Request,
    Res extends GetOut<Response, Req['type']>
    >(request: Req): Promise<Res> {
    return fetch(request.url)
        .then(response => response.json())
        .then(data => {
            return <Res>{
                type: request.type,
                data
            }
        });
}

// compiler knows that this is of type Promise<ResponseA> based on
// type discriminiator
let names = fetchData({
    type: 'names',
    url: '/names'
});

// compiler knows that this is of type Promise<ResponseB> based on
// type discriminiator
let numbers = fetchData({
    type: 'numbers',
    url: '/numbers'
});

0 个答案:

没有答案