diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 2cb6ab8f73ce9..8f0f0f5b52a32 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -28176,7 +28176,7 @@ namespace ts { let name: Expression | BindingName | undefined; let decl: Node | undefined; if (isVariableDeclaration(node.parent) && node.parent.initializer === node) { - if (!isInJSFile(node) && !isVarConst(node.parent)) { + if (!isInJSFile(node) && !(isVarConst(node.parent) && isFunctionLikeDeclaration(node))) { return undefined; } name = node.parent.name; diff --git a/tests/baselines/reference/classExpressionAssignment.errors.txt b/tests/baselines/reference/classExpressionAssignment.errors.txt new file mode 100644 index 0000000000000..2519e23b441aa --- /dev/null +++ b/tests/baselines/reference/classExpressionAssignment.errors.txt @@ -0,0 +1,16 @@ +tests/cases/compiler/classExpressionAssignment.ts(6,7): error TS2322: Type 'typeof A' is not assignable to type 'new () => A'. + Property 'prop' is missing in type 'A' but required in type 'A'. + + +==== tests/cases/compiler/classExpressionAssignment.ts (1 errors) ==== + interface A { + prop: string; + } + + // This is invalid + const A: {new(): A} = class {} + ~ +!!! error TS2322: Type 'typeof A' is not assignable to type 'new () => A'. +!!! error TS2322: Property 'prop' is missing in type 'A' but required in type 'A'. +!!! related TS2728 tests/cases/compiler/classExpressionAssignment.ts:2:3: 'prop' is declared here. + \ No newline at end of file diff --git a/tests/baselines/reference/classExpressionAssignment.js b/tests/baselines/reference/classExpressionAssignment.js new file mode 100644 index 0000000000000..8d924cfc44c98 --- /dev/null +++ b/tests/baselines/reference/classExpressionAssignment.js @@ -0,0 +1,16 @@ +//// [classExpressionAssignment.ts] +interface A { + prop: string; +} + +// This is invalid +const A: {new(): A} = class {} + + +//// [classExpressionAssignment.js] +// This is invalid +var A = /** @class */ (function () { + function A() { + } + return A; +}()); diff --git a/tests/baselines/reference/classExpressionAssignment.symbols b/tests/baselines/reference/classExpressionAssignment.symbols new file mode 100644 index 0000000000000..a1d8abd41cfe2 --- /dev/null +++ b/tests/baselines/reference/classExpressionAssignment.symbols @@ -0,0 +1,13 @@ +=== tests/cases/compiler/classExpressionAssignment.ts === +interface A { +>A : Symbol(A, Decl(classExpressionAssignment.ts, 0, 0), Decl(classExpressionAssignment.ts, 5, 5)) + + prop: string; +>prop : Symbol(A.prop, Decl(classExpressionAssignment.ts, 0, 13)) +} + +// This is invalid +const A: {new(): A} = class {} +>A : Symbol(A, Decl(classExpressionAssignment.ts, 0, 0), Decl(classExpressionAssignment.ts, 5, 5)) +>A : Symbol(A, Decl(classExpressionAssignment.ts, 0, 0), Decl(classExpressionAssignment.ts, 5, 5)) + diff --git a/tests/baselines/reference/classExpressionAssignment.types b/tests/baselines/reference/classExpressionAssignment.types new file mode 100644 index 0000000000000..cfbf796d76dff --- /dev/null +++ b/tests/baselines/reference/classExpressionAssignment.types @@ -0,0 +1,11 @@ +=== tests/cases/compiler/classExpressionAssignment.ts === +interface A { + prop: string; +>prop : string +} + +// This is invalid +const A: {new(): A} = class {} +>A : new () => A +>class {} : typeof A + diff --git a/tests/cases/compiler/classExpressionAssignment.ts b/tests/cases/compiler/classExpressionAssignment.ts new file mode 100644 index 0000000000000..4ad56188a767d --- /dev/null +++ b/tests/cases/compiler/classExpressionAssignment.ts @@ -0,0 +1,6 @@ +interface A { + prop: string; +} + +// This is invalid +const A: {new(): A} = class {}