Skip to content

Commit 16f69da

Browse files
committed
Merge pull request #5469 from Microsoft/improve-constructor-object-return-error-message
Improve constructor object return error message
2 parents 43b17c1 + 1b0bc8a commit 16f69da

File tree

5 files changed

+51
-5
lines changed

5 files changed

+51
-5
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12647,7 +12647,7 @@ namespace ts {
1264712647
error(node.expression, Diagnostics.Setters_cannot_return_a_value);
1264812648
}
1264912649
else if (func.kind === SyntaxKind.Constructor) {
12650-
if (!isTypeAssignableTo(exprType, returnType)) {
12650+
if (!checkTypeAssignableTo(exprType, returnType, node.expression)) {
1265112651
error(node.expression, Diagnostics.Return_type_of_constructor_signature_must_be_assignable_to_the_instance_type_of_the_class);
1265212652
}
1265312653
}

tests/baselines/reference/constructorReturnsInvalidType.errors.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1+
tests/cases/compiler/constructorReturnsInvalidType.ts(3,16): error TS2322: Type 'number' is not assignable to type 'X'.
2+
Property 'foo' is missing in type 'Number'.
13
tests/cases/compiler/constructorReturnsInvalidType.ts(3,16): error TS2409: Return type of constructor signature must be assignable to the instance type of the class
24

35

4-
==== tests/cases/compiler/constructorReturnsInvalidType.ts (1 errors) ====
6+
==== tests/cases/compiler/constructorReturnsInvalidType.ts (2 errors) ====
57
class X {
68
constructor() {
79
return 1;
810
~
11+
!!! error TS2322: Type 'number' is not assignable to type 'X'.
12+
!!! error TS2322: Property 'foo' is missing in type 'Number'.
13+
~
914
!!! error TS2409: Return type of constructor signature must be assignable to the instance type of the class
1015
}
1116
foo() { }

tests/baselines/reference/constructorWithAssignableReturnExpression.errors.txt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
tests/cases/conformance/classes/constructorDeclarations/constructorWithAssignableReturnExpression.ts(12,16): error TS2322: Type 'number' is not assignable to type 'D'.
2+
Property 'x' is missing in type 'Number'.
13
tests/cases/conformance/classes/constructorDeclarations/constructorWithAssignableReturnExpression.ts(12,16): error TS2409: Return type of constructor signature must be assignable to the instance type of the class
4+
tests/cases/conformance/classes/constructorDeclarations/constructorWithAssignableReturnExpression.ts(26,16): error TS2322: Type '{ x: number; }' is not assignable to type 'F<T>'.
5+
Types of property 'x' are incompatible.
6+
Type 'number' is not assignable to type 'T'.
27
tests/cases/conformance/classes/constructorDeclarations/constructorWithAssignableReturnExpression.ts(26,16): error TS2409: Return type of constructor signature must be assignable to the instance type of the class
38

49

5-
==== tests/cases/conformance/classes/constructorDeclarations/constructorWithAssignableReturnExpression.ts (2 errors) ====
10+
==== tests/cases/conformance/classes/constructorDeclarations/constructorWithAssignableReturnExpression.ts (4 errors) ====
611
// a class constructor may return an expression, it must be assignable to the class instance type to be valid
712

813
class C {
@@ -16,6 +21,9 @@ tests/cases/conformance/classes/constructorDeclarations/constructorWithAssignabl
1621
constructor() {
1722
return 1; // error
1823
~
24+
!!! error TS2322: Type 'number' is not assignable to type 'D'.
25+
!!! error TS2322: Property 'x' is missing in type 'Number'.
26+
~
1927
!!! error TS2409: Return type of constructor signature must be assignable to the instance type of the class
2028
}
2129
}
@@ -32,6 +40,10 @@ tests/cases/conformance/classes/constructorDeclarations/constructorWithAssignabl
3240
constructor() {
3341
return { x: 1 }; // error
3442
~~~~~~~~
43+
!!! error TS2322: Type '{ x: number; }' is not assignable to type 'F<T>'.
44+
!!! error TS2322: Types of property 'x' are incompatible.
45+
!!! error TS2322: Type 'number' is not assignable to type 'T'.
46+
~~~~~~~~
3547
!!! error TS2409: Return type of constructor signature must be assignable to the instance type of the class
3648
}
3749
}

tests/baselines/reference/returnInConstructor1.errors.txt

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1+
tests/cases/compiler/returnInConstructor1.ts(11,16): error TS2322: Type 'number' is not assignable to type 'B'.
2+
Property 'foo' is missing in type 'Number'.
13
tests/cases/compiler/returnInConstructor1.ts(11,16): error TS2409: Return type of constructor signature must be assignable to the instance type of the class
4+
tests/cases/compiler/returnInConstructor1.ts(25,16): error TS2322: Type 'string' is not assignable to type 'D'.
5+
Property 'foo' is missing in type 'String'.
26
tests/cases/compiler/returnInConstructor1.ts(25,16): error TS2409: Return type of constructor signature must be assignable to the instance type of the class
7+
tests/cases/compiler/returnInConstructor1.ts(39,16): error TS2322: Type '{ foo: number; }' is not assignable to type 'F'.
8+
Types of property 'foo' are incompatible.
9+
Type 'number' is not assignable to type 'string'.
310
tests/cases/compiler/returnInConstructor1.ts(39,16): error TS2409: Return type of constructor signature must be assignable to the instance type of the class
11+
tests/cases/compiler/returnInConstructor1.ts(55,16): error TS2322: Type 'G' is not assignable to type 'H'.
12+
Types of property 'foo' are incompatible.
13+
Type '() => void' is not assignable to type 'string'.
414
tests/cases/compiler/returnInConstructor1.ts(55,16): error TS2409: Return type of constructor signature must be assignable to the instance type of the class
515

616

7-
==== tests/cases/compiler/returnInConstructor1.ts (4 errors) ====
17+
==== tests/cases/compiler/returnInConstructor1.ts (8 errors) ====
818
class A {
919
foo() { }
1020
constructor() {
@@ -17,6 +27,9 @@ tests/cases/compiler/returnInConstructor1.ts(55,16): error TS2409: Return type o
1727
constructor() {
1828
return 1; // error
1929
~
30+
!!! error TS2322: Type 'number' is not assignable to type 'B'.
31+
!!! error TS2322: Property 'foo' is missing in type 'Number'.
32+
~
2033
!!! error TS2409: Return type of constructor signature must be assignable to the instance type of the class
2134
}
2235
}
@@ -33,6 +46,9 @@ tests/cases/compiler/returnInConstructor1.ts(55,16): error TS2409: Return type o
3346
constructor() {
3447
return "test"; // error
3548
~~~~~~
49+
!!! error TS2322: Type 'string' is not assignable to type 'D'.
50+
!!! error TS2322: Property 'foo' is missing in type 'String'.
51+
~~~~~~
3652
!!! error TS2409: Return type of constructor signature must be assignable to the instance type of the class
3753
}
3854
}
@@ -49,6 +65,10 @@ tests/cases/compiler/returnInConstructor1.ts(55,16): error TS2409: Return type o
4965
constructor() {
5066
return { foo: 1 }; //error
5167
~~~~~~~~~~
68+
!!! error TS2322: Type '{ foo: number; }' is not assignable to type 'F'.
69+
!!! error TS2322: Types of property 'foo' are incompatible.
70+
!!! error TS2322: Type 'number' is not assignable to type 'string'.
71+
~~~~~~~~~~
5272
!!! error TS2409: Return type of constructor signature must be assignable to the instance type of the class
5373
}
5474
}
@@ -67,6 +87,10 @@ tests/cases/compiler/returnInConstructor1.ts(55,16): error TS2409: Return type o
6787
super();
6888
return new G(); //error
6989
~~~~~~~
90+
!!! error TS2322: Type 'G' is not assignable to type 'H'.
91+
!!! error TS2322: Types of property 'foo' are incompatible.
92+
!!! error TS2322: Type '() => void' is not assignable to type 'string'.
93+
~~~~~~~
7094
!!! error TS2409: Return type of constructor signature must be assignable to the instance type of the class
7195
}
7296
}

tests/baselines/reference/typeGuardFunctionErrors.errors.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(96,9):
2525
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(97,16): error TS1228: A type predicate is only allowed in return type position for functions and methods.
2626
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(98,20): error TS1228: A type predicate is only allowed in return type position for functions and methods.
2727
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(104,25): error TS1228: A type predicate is only allowed in return type position for functions and methods.
28+
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(105,16): error TS2322: Type 'boolean' is not assignable to type 'D'.
29+
Property 'm1' is missing in type 'Boolean'.
2830
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(105,16): error TS2409: Return type of constructor signature must be assignable to the instance type of the class
2931
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(107,20): error TS1228: A type predicate is only allowed in return type position for functions and methods.
3032
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(110,20): error TS1228: A type predicate is only allowed in return type position for functions and methods.
@@ -37,7 +39,7 @@ tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(133,34
3739
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(137,39): error TS1230: A type predicate cannot reference element 'p1' in a binding pattern.
3840

3941

40-
==== tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts (31 errors) ====
42+
==== tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts (32 errors) ====
4143

4244
class A {
4345
propA: number;
@@ -192,6 +194,9 @@ tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(137,39
192194
!!! error TS1228: A type predicate is only allowed in return type position for functions and methods.
193195
return true;
194196
~~~~
197+
!!! error TS2322: Type 'boolean' is not assignable to type 'D'.
198+
!!! error TS2322: Property 'm1' is missing in type 'Boolean'.
199+
~~~~
195200
!!! error TS2409: Return type of constructor signature must be assignable to the instance type of the class
196201
}
197202
get m1(p1: A): p1 is C {

0 commit comments

Comments
 (0)