type A = Promise<string> | Promise<number>
type B = Promise<string | number>
const a: A = Math.random() > 0.5 ? Promise.resolve('string') : Promise.resolve(1)
const b: B = Promise.resolve(Math.random() > 0.5 ? 'string' : 1)
class C {
fa(a: A) {
console.log(a)
}
fb(b: B) {
console.log(b)
}
}
const c = new C()
c.fa(a)
c.fb(b)
c.fa(b) /* <-- This produces an error:
Argument of type 'B' is not assignable to parameter of type 'A'.
Type 'Promise<string | number>' is not assignable to type 'Promise<string>'.
Type 'string | number' is not assignable to type 'string'.
Type 'number' is not assignable to type 'string'.ts(2345)
*/
c.fb(a) // This works
Why does this code produce an error?
P.S. Typescript 4.9.4