After compiling the code below, it runs without any issues
interface A {
x: string;
}
interface B {
x: string;
}
type C<D extends A> = D | B;
declare function funcA<D extends A, E extends C<D> = C<D>>(): E | E[] | undefined;
declare function funcB<D extends A, E extends C<D> = C<D>>(param?: E | E[]): void;
const varX = funcA();
funcB(varX);
However, interesting things start happening when the function funcB
is enclosed within a class:
export class MyClass<D extends A, E extends C<D>> {
functionF(param?: E | E[]): void {}
functionG() {
const varY = funcA();
this.functionF(funcA());
this.functionF(varY);
}
}
According to the code, the function MyClass.functionF
should be the same as function funcB
. Nonetheless, an error pops up at the line this.functionF(varY)
. Interestingly, the line this.functionF(funcA())
compiles without any issues.
Argument of type 'B | A | (B | A)[] | undefined' is not assignable to parameter of type 'E | E[] | undefined'.
Type 'B' is not assignable to type 'E | E[] | undefined'.
Type 'B' is missing properties like length, pop, push, concat, and more which are required by type 'E[]'.
This error is perplexing because type E
should be equivalent to D | A
, so how come A
cannot be assigned to E
?