interface A {
    A: string;
};
interface B {
    B: string;
}
function returnTwoType<T extends A | B>(type: 'A' | 'B'): T {
    if (type === 'A') {
        const res = { A: '类型 A' };
        // Type '{ A: string; }' is not assignable to type 'T'.
        // '{ A: string; }' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'A | B'.
        return res
    }
    else {
        const res = {
            B: '类型 B'
        };
        // Type '{ B: string; }' is not assignable to type 'T'.
        // '{ B: string; }' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'A | B'.
        return res
    }
}
const a = returnTwoType<A>('A')
const b = returnTwoType<B>('B')
|  |      1Zhuzhuchenyan      2021-09-18 17:24:10 +08:00 可能 Conditional Types 可以帮到你 https://www.typescriptlang.org/docs/handbook/2/conditional-types.html |