Skip to content

Fix class expression from being assignable if types don't match #40660

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

Merged
merged 3 commits into from
Oct 5, 2020
Merged
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
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions tests/baselines/reference/classExpressionAssignment.errors.txt
Original file line number Diff line number Diff line change
@@ -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.

16 changes: 16 additions & 0 deletions tests/baselines/reference/classExpressionAssignment.js
Original file line number Diff line number Diff line change
@@ -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;
}());
13 changes: 13 additions & 0 deletions tests/baselines/reference/classExpressionAssignment.symbols
Original file line number Diff line number Diff line change
@@ -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))

11 changes: 11 additions & 0 deletions tests/baselines/reference/classExpressionAssignment.types
Original file line number Diff line number Diff line change
@@ -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

6 changes: 6 additions & 0 deletions tests/cases/compiler/classExpressionAssignment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
interface A {
prop: string;
}

// This is invalid
const A: {new(): A} = class {}