Skip to content

Commit 1191e2e

Browse files
Fix class expression from being assignable if types don't match (#40660)
* Fix class expression from being assignable if types don't match * Fix class expression from being assignable if types don't match
1 parent 77df9fa commit 1191e2e

6 files changed

+63
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28180,7 +28180,7 @@ namespace ts {
2818028180
let name: Expression | BindingName | undefined;
2818128181
let decl: Node | undefined;
2818228182
if (isVariableDeclaration(node.parent) && node.parent.initializer === node) {
28183-
if (!isInJSFile(node) && !isVarConst(node.parent)) {
28183+
if (!isInJSFile(node) && !(isVarConst(node.parent) && isFunctionLikeDeclaration(node))) {
2818428184
return undefined;
2818528185
}
2818628186
name = node.parent.name;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
tests/cases/compiler/classExpressionAssignment.ts(6,7): error TS2322: Type 'typeof A' is not assignable to type 'new () => A'.
2+
Property 'prop' is missing in type 'A' but required in type 'A'.
3+
4+
5+
==== tests/cases/compiler/classExpressionAssignment.ts (1 errors) ====
6+
interface A {
7+
prop: string;
8+
}
9+
10+
// This is invalid
11+
const A: {new(): A} = class {}
12+
~
13+
!!! error TS2322: Type 'typeof A' is not assignable to type 'new () => A'.
14+
!!! error TS2322: Property 'prop' is missing in type 'A' but required in type 'A'.
15+
!!! related TS2728 tests/cases/compiler/classExpressionAssignment.ts:2:3: 'prop' is declared here.
16+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//// [classExpressionAssignment.ts]
2+
interface A {
3+
prop: string;
4+
}
5+
6+
// This is invalid
7+
const A: {new(): A} = class {}
8+
9+
10+
//// [classExpressionAssignment.js]
11+
// This is invalid
12+
var A = /** @class */ (function () {
13+
function A() {
14+
}
15+
return A;
16+
}());
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/classExpressionAssignment.ts ===
2+
interface A {
3+
>A : Symbol(A, Decl(classExpressionAssignment.ts, 0, 0), Decl(classExpressionAssignment.ts, 5, 5))
4+
5+
prop: string;
6+
>prop : Symbol(A.prop, Decl(classExpressionAssignment.ts, 0, 13))
7+
}
8+
9+
// This is invalid
10+
const A: {new(): A} = class {}
11+
>A : Symbol(A, Decl(classExpressionAssignment.ts, 0, 0), Decl(classExpressionAssignment.ts, 5, 5))
12+
>A : Symbol(A, Decl(classExpressionAssignment.ts, 0, 0), Decl(classExpressionAssignment.ts, 5, 5))
13+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
=== tests/cases/compiler/classExpressionAssignment.ts ===
2+
interface A {
3+
prop: string;
4+
>prop : string
5+
}
6+
7+
// This is invalid
8+
const A: {new(): A} = class {}
9+
>A : new () => A
10+
>class {} : typeof A
11+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
interface A {
2+
prop: string;
3+
}
4+
5+
// This is invalid
6+
const A: {new(): A} = class {}

0 commit comments

Comments
 (0)