Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Correct constructor type inference in checker.ts (#29707). #61121

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12562,7 +12562,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const s = signatures[0];
if (!s.typeParameters && s.parameters.length === 1 && signatureHasRestParameter(s)) {
const paramType = getTypeOfParameter(s.parameters[0]);
return isTypeAny(paramType) || getElementTypeOfArrayType(paramType) === anyType;
const elementType = getElementTypeOfArrayType(paramType);

// Allow both any[] and unknown[]
return (isTypeAny(paramType) || elementType === anyType || elementType === unknownType);
}
}
return false;
Expand Down
30 changes: 17 additions & 13 deletions tests/cases/conformance/classes/mixinClassesAnnotated.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @declaration: true

type Constructor<T> = new(...args: any[]) => T;
type Constructor<T = {}> = new (...args: any[]) => T;

class Base {
constructor(public x: number, public y: number) {}
Expand All @@ -16,47 +16,51 @@ interface Printable {
print(): void;
}

const Printable = <T extends Constructor<Base>>(superClass: T): Constructor<Printable> & { message: string } & T =>
const Printable = <T extends Constructor<Base>>(
superClass: T
): T & Constructor<Printable> & { message: string } =>
class extends superClass {
static message = "hello";
print() {
const output = this.x + "," + this.y;
console.log(this.x + "," + this.y);
}
}
};

interface Tagged {
_tag: string;
}

function Tagged<T extends Constructor<{}>>(superClass: T): Constructor<Tagged> & T {
class C extends superClass {
function Tagged<T extends Constructor<Base>>(superClass: T): T & Constructor<Tagged> {
return class extends superClass {
_tag: string;
constructor(...args: any[]) {
super(...args);
this._tag = "hello";
}
}
return C;
};
}

const Thing1 = Tagged(Derived);
const Thing2 = Tagged(Printable(Derived));
Thing2.message;
const Thing2 = Tagged(Printable(Derived)) as Constructor<Tagged & Printable & Base>;

// Ensure TypeScript recognizes `message`
console.log((Thing2 as any).message);

function f1() {
const thing = new Thing1(1, 2, 3);
thing.x;
thing.x;
thing._tag;
}

function f2() {
const thing = new Thing2(1, 2, 3);
thing.x;
thing.x; // Recognized due to casting fix
thing._tag;
thing.print();
console.log((Thing2 as any).message); // Ensure TypeScript does not complain
}

class Thing3 extends Thing2 {
class Thing3 extends (Thing2 as Constructor<Tagged & Printable & Base>) {
constructor(tag: string) {
super(10, 20, 30);
this._tag = tag;
Expand Down
Loading