'Infer return type from input type with discriminated unions

Looking at this example can I somehow (probably with a generic) infer ResultA based on the input parameter?

type AorB = A | B

type A = {
    type: 'a'
    data: 'data'
}

type B = {
    type: 'b'
    data: 'data'
}

type ResultAorB = ResultA | ResultB

type ResultA = {
    type: 'a'
    data: 'data'
}

type ResultB = {
    type: 'b'
    data: 'data'
}

function doSomething(input: AorB): ResultAorB {
    return input
}

// result is ResultAorB, but I somehow want to infer that it should be ResultA
const result: ResultA = doSomething({type: 'a', data: 'data'})


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source