mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-15 12:51:30 -05:00
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
This commit is contained in:
@@ -28180,7 +28180,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;
|
||||
|
||||
@@ -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
tests/baselines/reference/classExpressionAssignment.js
Normal file
16
tests/baselines/reference/classExpressionAssignment.js
Normal file
@@ -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
tests/baselines/reference/classExpressionAssignment.symbols
Normal file
13
tests/baselines/reference/classExpressionAssignment.symbols
Normal file
@@ -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
tests/baselines/reference/classExpressionAssignment.types
Normal file
11
tests/baselines/reference/classExpressionAssignment.types
Normal file
@@ -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
tests/cases/compiler/classExpressionAssignment.ts
Normal file
6
tests/cases/compiler/classExpressionAssignment.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
interface A {
|
||||
prop: string;
|
||||
}
|
||||
|
||||
// This is invalid
|
||||
const A: {new(): A} = class {}
|
||||
Reference in New Issue
Block a user