Skip to content

Commit bb369f1

Browse files
committed
Merge pull request microsoft#5290 from Microsoft/interfaceClassMergingFix
Do not report errors for classes and interfaces merging
2 parents a8aa48e + 8b8d33d commit bb369f1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1076
-304
lines changed

src/compiler/checker.ts

-15
Original file line numberDiff line numberDiff line change
@@ -12875,11 +12875,6 @@ namespace ts {
1287512875
}
1287612876
checkClassLikeDeclaration(node);
1287712877

12878-
// Interfaces cannot be merged with non-ambient classes.
12879-
if (getSymbolOfNode(node).flags & SymbolFlags.Interface && !isInAmbientContext(node)) {
12880-
error(node, Diagnostics.Only_an_ambient_class_can_be_merged_with_an_interface);
12881-
}
12882-
1288312878
forEach(node.members, checkSourceElement);
1288412879
}
1288512880

@@ -13165,16 +13160,6 @@ namespace ts {
1316513160
checkIndexConstraints(type);
1316613161
}
1316713162
}
13168-
13169-
// Interfaces cannot merge with non-ambient classes.
13170-
if (symbol && symbol.declarations) {
13171-
for (let declaration of symbol.declarations) {
13172-
if (declaration.kind === SyntaxKind.ClassDeclaration && !isInAmbientContext(declaration)) {
13173-
error(node, Diagnostics.Only_an_ambient_class_can_be_merged_with_an_interface);
13174-
break;
13175-
}
13176-
}
13177-
}
1317813163
}
1317913164
forEach(getInterfaceBaseTypeNodes(node), heritageElement => {
1318013165
if (!isSupportedExpressionWithTypeArguments(heritageElement)) {

src/compiler/diagnosticMessages.json

-4
Original file line numberDiff line numberDiff line change
@@ -1620,10 +1620,6 @@
16201620
"category": "Error",
16211621
"code":2517
16221622
},
1623-
"Only an ambient class can be merged with an interface.": {
1624-
"category": "Error",
1625-
"code": 2518
1626-
},
16271623
"Duplicate identifier '{0}'. Compiler uses declaration '{1}' to support async functions.": {
16281624
"category": "Error",
16291625
"code": 2520

tests/baselines/reference/augmentedTypesClass2.errors.txt

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
1-
tests/cases/compiler/augmentedTypesClass2.ts(4,7): error TS2518: Only an ambient class can be merged with an interface.
2-
tests/cases/compiler/augmentedTypesClass2.ts(10,11): error TS2518: Only an ambient class can be merged with an interface.
31
tests/cases/compiler/augmentedTypesClass2.ts(16,7): error TS2300: Duplicate identifier 'c33'.
42
tests/cases/compiler/augmentedTypesClass2.ts(21,6): error TS2300: Duplicate identifier 'c33'.
53

64

7-
==== tests/cases/compiler/augmentedTypesClass2.ts (4 errors) ====
5+
==== tests/cases/compiler/augmentedTypesClass2.ts (2 errors) ====
86
// Checking class with other things in type space not value space
97

108
// class then interface
11-
class c11 { // error
12-
~~~
13-
!!! error TS2518: Only an ambient class can be merged with an interface.
9+
class c11 {
1410
foo() {
1511
return 1;
1612
}
1713
}
1814

19-
interface c11 { // error
20-
~~~
21-
!!! error TS2518: Only an ambient class can be merged with an interface.
15+
interface c11 {
2216
bar(): void;
2317
}
2418

tests/baselines/reference/augmentedTypesClass2.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
// Checking class with other things in type space not value space
33

44
// class then interface
5-
class c11 { // error
5+
class c11 {
66
foo() {
77
return 1;
88
}
99
}
1010

11-
interface c11 { // error
11+
interface c11 {
1212
bar(): void;
1313
}
1414

tests/baselines/reference/augmentedTypesInterface.errors.txt

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
tests/cases/compiler/augmentedTypesInterface.ts(12,11): error TS2518: Only an ambient class can be merged with an interface.
2-
tests/cases/compiler/augmentedTypesInterface.ts(16,7): error TS2518: Only an ambient class can be merged with an interface.
31
tests/cases/compiler/augmentedTypesInterface.ts(23,11): error TS2300: Duplicate identifier 'i3'.
42
tests/cases/compiler/augmentedTypesInterface.ts(26,6): error TS2300: Duplicate identifier 'i3'.
53

64

7-
==== tests/cases/compiler/augmentedTypesInterface.ts (4 errors) ====
5+
==== tests/cases/compiler/augmentedTypesInterface.ts (2 errors) ====
86
// interface then interface
97

108
interface i {
@@ -16,15 +14,11 @@ tests/cases/compiler/augmentedTypesInterface.ts(26,6): error TS2300: Duplicate i
1614
}
1715

1816
// interface then class
19-
interface i2 { // error
20-
~~
21-
!!! error TS2518: Only an ambient class can be merged with an interface.
17+
interface i2 {
2218
foo(): void;
2319
}
2420

25-
class i2 { // error
26-
~~
27-
!!! error TS2518: Only an ambient class can be merged with an interface.
21+
class i2 {
2822
bar() {
2923
return 1;
3024
}

tests/baselines/reference/augmentedTypesInterface.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ interface i {
1010
}
1111

1212
// interface then class
13-
interface i2 { // error
13+
interface i2 {
1414
foo(): void;
1515
}
1616

17-
class i2 { // error
17+
class i2 {
1818
bar() {
1919
return 1;
2020
}

tests/baselines/reference/classAbstractMergedDeclaration.errors.txt

+1-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
tests/cases/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractMergedDeclaration.ts(7,16): error TS2518: Only an ambient class can be merged with an interface.
2-
tests/cases/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractMergedDeclaration.ts(8,11): error TS2518: Only an ambient class can be merged with an interface.
3-
tests/cases/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractMergedDeclaration.ts(10,11): error TS2518: Only an ambient class can be merged with an interface.
4-
tests/cases/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractMergedDeclaration.ts(11,16): error TS2518: Only an ambient class can be merged with an interface.
51
tests/cases/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractMergedDeclaration.ts(13,16): error TS2300: Duplicate identifier 'CC1'.
62
tests/cases/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractMergedDeclaration.ts(14,7): error TS2300: Duplicate identifier 'CC1'.
73
tests/cases/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractMergedDeclaration.ts(16,7): error TS2300: Duplicate identifier 'CC2'.
@@ -20,26 +16,18 @@ tests/cases/conformance/classes/classDeclarations/classAbstractKeyword/classAbst
2016
tests/cases/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractMergedDeclaration.ts(39,1): error TS2511: Cannot create an instance of the abstract class 'DCC1'.
2117

2218

23-
==== tests/cases/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractMergedDeclaration.ts (20 errors) ====
19+
==== tests/cases/conformance/classes/classDeclarations/classAbstractKeyword/classAbstractMergedDeclaration.ts (16 errors) ====
2420
abstract class CM {}
2521
module CM {}
2622

2723
module MC {}
2824
abstract class MC {}
2925

3026
abstract class CI {}
31-
~~
32-
!!! error TS2518: Only an ambient class can be merged with an interface.
3327
interface CI {}
34-
~~
35-
!!! error TS2518: Only an ambient class can be merged with an interface.
3628

3729
interface IC {}
38-
~~
39-
!!! error TS2518: Only an ambient class can be merged with an interface.
4030
abstract class IC {}
41-
~~
42-
!!! error TS2518: Only an ambient class can be merged with an interface.
4331

4432
abstract class CC1 {}
4533
~~~

tests/baselines/reference/classAndInterfaceWithSameName.errors.txt

+3-15
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,25 @@
1-
tests/cases/conformance/classes/classDeclarations/classAndInterfaceWithSameName.ts(1,7): error TS2518: Only an ambient class can be merged with an interface.
21
tests/cases/conformance/classes/classDeclarations/classAndInterfaceWithSameName.ts(1,11): error TS2300: Duplicate identifier 'foo'.
3-
tests/cases/conformance/classes/classDeclarations/classAndInterfaceWithSameName.ts(2,11): error TS2518: Only an ambient class can be merged with an interface.
42
tests/cases/conformance/classes/classDeclarations/classAndInterfaceWithSameName.ts(2,15): error TS2300: Duplicate identifier 'foo'.
5-
tests/cases/conformance/classes/classDeclarations/classAndInterfaceWithSameName.ts(5,11): error TS2518: Only an ambient class can be merged with an interface.
63
tests/cases/conformance/classes/classDeclarations/classAndInterfaceWithSameName.ts(6,9): error TS2300: Duplicate identifier 'bar'.
7-
tests/cases/conformance/classes/classDeclarations/classAndInterfaceWithSameName.ts(9,15): error TS2518: Only an ambient class can be merged with an interface.
84
tests/cases/conformance/classes/classDeclarations/classAndInterfaceWithSameName.ts(10,9): error TS2300: Duplicate identifier 'bar'.
95

106

11-
==== tests/cases/conformance/classes/classDeclarations/classAndInterfaceWithSameName.ts (8 errors) ====
7+
==== tests/cases/conformance/classes/classDeclarations/classAndInterfaceWithSameName.ts (4 errors) ====
128
class C { foo: string; }
13-
~
14-
!!! error TS2518: Only an ambient class can be merged with an interface.
159
~~~
1610
!!! error TS2300: Duplicate identifier 'foo'.
17-
interface C { foo: string; } // error
18-
~
19-
!!! error TS2518: Only an ambient class can be merged with an interface.
11+
interface C { foo: string; }
2012
~~~
2113
!!! error TS2300: Duplicate identifier 'foo'.
2214

2315
module M {
2416
class D {
25-
~
26-
!!! error TS2518: Only an ambient class can be merged with an interface.
2717
bar: string;
2818
~~~
2919
!!! error TS2300: Duplicate identifier 'bar'.
3020
}
3121

32-
interface D { // error
33-
~
34-
!!! error TS2518: Only an ambient class can be merged with an interface.
22+
interface D {
3523
bar: string;
3624
~~~
3725
!!! error TS2300: Duplicate identifier 'bar'.

tests/baselines/reference/classAndInterfaceWithSameName.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
//// [classAndInterfaceWithSameName.ts]
22
class C { foo: string; }
3-
interface C { foo: string; } // error
3+
interface C { foo: string; }
44

55
module M {
66
class D {
77
bar: string;
88
}
99

10-
interface D { // error
10+
interface D {
1111
bar: string;
1212
}
1313
}

tests/baselines/reference/clinterfaces.errors.txt

-52
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
=== tests/cases/compiler/clinterfaces.ts ===
2+
module M {
3+
>M : Symbol(M, Decl(clinterfaces.ts, 0, 0))
4+
5+
class C { }
6+
>C : Symbol(C, Decl(clinterfaces.ts, 0, 10), Decl(clinterfaces.ts, 1, 15))
7+
8+
interface C { }
9+
>C : Symbol(C, Decl(clinterfaces.ts, 0, 10), Decl(clinterfaces.ts, 1, 15))
10+
11+
interface D { }
12+
>D : Symbol(D, Decl(clinterfaces.ts, 2, 19), Decl(clinterfaces.ts, 3, 19))
13+
14+
class D { }
15+
>D : Symbol(D, Decl(clinterfaces.ts, 2, 19), Decl(clinterfaces.ts, 3, 19))
16+
}
17+
18+
interface Foo<T> {
19+
>Foo : Symbol(Foo, Decl(clinterfaces.ts, 5, 1), Decl(clinterfaces.ts, 9, 1))
20+
>T : Symbol(T, Decl(clinterfaces.ts, 7, 14), Decl(clinterfaces.ts, 11, 10))
21+
22+
a: string;
23+
>a : Symbol(a, Decl(clinterfaces.ts, 7, 18))
24+
}
25+
26+
class Foo<T>{
27+
>Foo : Symbol(Foo, Decl(clinterfaces.ts, 5, 1), Decl(clinterfaces.ts, 9, 1))
28+
>T : Symbol(T, Decl(clinterfaces.ts, 7, 14), Decl(clinterfaces.ts, 11, 10))
29+
30+
b: number;
31+
>b : Symbol(b, Decl(clinterfaces.ts, 11, 13))
32+
}
33+
34+
class Bar<T>{
35+
>Bar : Symbol(Bar, Decl(clinterfaces.ts, 13, 1), Decl(clinterfaces.ts, 17, 1))
36+
>T : Symbol(T, Decl(clinterfaces.ts, 15, 10), Decl(clinterfaces.ts, 19, 14))
37+
38+
b: number;
39+
>b : Symbol(b, Decl(clinterfaces.ts, 15, 13))
40+
}
41+
42+
interface Bar<T> {
43+
>Bar : Symbol(Bar, Decl(clinterfaces.ts, 13, 1), Decl(clinterfaces.ts, 17, 1))
44+
>T : Symbol(T, Decl(clinterfaces.ts, 15, 10), Decl(clinterfaces.ts, 19, 14))
45+
46+
a: string;
47+
>a : Symbol(a, Decl(clinterfaces.ts, 19, 18))
48+
}
49+
50+
export = Foo;
51+
>Foo : Symbol(Foo, Decl(clinterfaces.ts, 5, 1), Decl(clinterfaces.ts, 9, 1))
52+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
=== tests/cases/compiler/clinterfaces.ts ===
2+
module M {
3+
>M : typeof M
4+
5+
class C { }
6+
>C : C
7+
8+
interface C { }
9+
>C : C
10+
11+
interface D { }
12+
>D : D
13+
14+
class D { }
15+
>D : D
16+
}
17+
18+
interface Foo<T> {
19+
>Foo : Foo<T>
20+
>T : T
21+
22+
a: string;
23+
>a : string
24+
}
25+
26+
class Foo<T>{
27+
>Foo : Foo<T>
28+
>T : T
29+
30+
b: number;
31+
>b : number
32+
}
33+
34+
class Bar<T>{
35+
>Bar : Bar<T>
36+
>T : T
37+
38+
b: number;
39+
>b : number
40+
}
41+
42+
interface Bar<T> {
43+
>Bar : Bar<T>
44+
>T : T
45+
46+
a: string;
47+
>a : string
48+
}
49+
50+
export = Foo;
51+
>Foo : Foo<T>
52+

0 commit comments

Comments
 (0)