Skip to content

Commit 0d27774

Browse files
authored
Port TS PR 60490 (Implementing a prim type in a class expression should report error) (#1255)
1 parent ed1c512 commit 0d27774

File tree

5 files changed

+40
-92
lines changed

5 files changed

+40
-92
lines changed

internal/checker/checker.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1600,9 +1600,9 @@ func (c *Checker) checkAndReportErrorForUsingTypeAsValue(errorLocation *ast.Node
16001600
containerKind := grandparent.Parent.Kind
16011601
if containerKind == ast.KindInterfaceDeclaration && heritageKind == ast.KindExtendsKeyword {
16021602
c.error(errorLocation, diagnostics.An_interface_cannot_extend_a_primitive_type_like_0_It_can_only_extend_other_named_object_types, name)
1603-
} else if containerKind == ast.KindClassDeclaration && heritageKind == ast.KindExtendsKeyword {
1603+
} else if ast.IsClassLike(grandparent.Parent) && heritageKind == ast.KindExtendsKeyword {
16041604
c.error(errorLocation, diagnostics.A_class_cannot_extend_a_primitive_type_like_0_Classes_can_only_extend_constructable_values, name)
1605-
} else if containerKind == ast.KindClassDeclaration && heritageKind == ast.KindImplementsKeyword {
1605+
} else if ast.IsClassLike(grandparent.Parent) && heritageKind == ast.KindImplementsKeyword {
16061606
c.error(errorLocation, diagnostics.A_class_cannot_implement_a_primitive_type_like_0_It_can_only_implement_other_named_object_types, name)
16071607
}
16081608
} else {

testdata/baselines/reference/submodule/compiler/classImplementsPrimitive.errors.txt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
classImplementsPrimitive.ts(3,20): error TS2864: A class cannot implement a primitive type like 'number'. It can only implement other named object types.
22
classImplementsPrimitive.ts(4,21): error TS2864: A class cannot implement a primitive type like 'string'. It can only implement other named object types.
33
classImplementsPrimitive.ts(5,21): error TS2864: A class cannot implement a primitive type like 'boolean'. It can only implement other named object types.
4+
classImplementsPrimitive.ts(7,29): error TS2864: A class cannot implement a primitive type like 'number'. It can only implement other named object types.
5+
classImplementsPrimitive.ts(8,29): error TS2864: A class cannot implement a primitive type like 'string'. It can only implement other named object types.
6+
classImplementsPrimitive.ts(9,29): error TS2864: A class cannot implement a primitive type like 'boolean'. It can only implement other named object types.
7+
classImplementsPrimitive.ts(11,31): error TS2864: A class cannot implement a primitive type like 'number'. It can only implement other named object types.
8+
classImplementsPrimitive.ts(12,31): error TS2864: A class cannot implement a primitive type like 'string'. It can only implement other named object types.
9+
classImplementsPrimitive.ts(13,31): error TS2864: A class cannot implement a primitive type like 'boolean'. It can only implement other named object types.
410

511

6-
==== classImplementsPrimitive.ts (3 errors) ====
12+
==== classImplementsPrimitive.ts (9 errors) ====
713
// classes cannot implement primitives
814

915
class C implements number { }
@@ -17,10 +23,22 @@ classImplementsPrimitive.ts(5,21): error TS2864: A class cannot implement a prim
1723
!!! error TS2864: A class cannot implement a primitive type like 'boolean'. It can only implement other named object types.
1824

1925
const C4 = class implements number {}
26+
~~~~~~
27+
!!! error TS2864: A class cannot implement a primitive type like 'number'. It can only implement other named object types.
2028
const C5 = class implements string {}
29+
~~~~~~
30+
!!! error TS2864: A class cannot implement a primitive type like 'string'. It can only implement other named object types.
2131
const C6 = class implements boolean {}
32+
~~~~~~~
33+
!!! error TS2864: A class cannot implement a primitive type like 'boolean'. It can only implement other named object types.
2234

2335
const C7 = class A implements number { }
36+
~~~~~~
37+
!!! error TS2864: A class cannot implement a primitive type like 'number'. It can only implement other named object types.
2438
const C8 = class B implements string { }
39+
~~~~~~
40+
!!! error TS2864: A class cannot implement a primitive type like 'string'. It can only implement other named object types.
2541
const C9 = class C implements boolean { }
42+
~~~~~~~
43+
!!! error TS2864: A class cannot implement a primitive type like 'boolean'. It can only implement other named object types.
2644

testdata/baselines/reference/submodule/compiler/classImplementsPrimitive.errors.txt.diff

Lines changed: 0 additions & 44 deletions
This file was deleted.

testdata/baselines/reference/submodule/conformance/classExtendingPrimitive.errors.txt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@ classExtendingPrimitive.ts(8,18): error TS2304: Cannot find name 'Null'.
77
classExtendingPrimitive.ts(10,18): error TS2507: Type 'undefined' is not a constructor function type.
88
classExtendingPrimitive.ts(11,18): error TS2552: Cannot find name 'Undefined'. Did you mean 'undefined'?
99
classExtendingPrimitive.ts(14,18): error TS2507: Type 'typeof E' is not a constructor function type.
10+
classExtendingPrimitive.ts(16,26): error TS2863: A class cannot extend a primitive type like 'number'. Classes can only extend constructable values.
11+
classExtendingPrimitive.ts(17,27): error TS2863: A class cannot extend a primitive type like 'string'. Classes can only extend constructable values.
12+
classExtendingPrimitive.ts(18,27): error TS2863: A class cannot extend a primitive type like 'boolean'. Classes can only extend constructable values.
13+
classExtendingPrimitive.ts(20,29): error TS2863: A class cannot extend a primitive type like 'number'. Classes can only extend constructable values.
14+
classExtendingPrimitive.ts(21,29): error TS2863: A class cannot extend a primitive type like 'string'. Classes can only extend constructable values.
15+
classExtendingPrimitive.ts(22,29): error TS2863: A class cannot extend a primitive type like 'boolean'. Classes can only extend constructable values.
1016

1117

12-
==== classExtendingPrimitive.ts (9 errors) ====
18+
==== classExtendingPrimitive.ts (15 errors) ====
1319
// classes cannot extend primitives
1420

1521
class C extends number { }
@@ -44,10 +50,22 @@ classExtendingPrimitive.ts(14,18): error TS2507: Type 'typeof E' is not a constr
4450
!!! error TS2507: Type 'typeof E' is not a constructor function type.
4551

4652
const C9 = class extends number { }
53+
~~~~~~
54+
!!! error TS2863: A class cannot extend a primitive type like 'number'. Classes can only extend constructable values.
4755
const C10 = class extends string { }
56+
~~~~~~
57+
!!! error TS2863: A class cannot extend a primitive type like 'string'. Classes can only extend constructable values.
4858
const C11 = class extends boolean { }
59+
~~~~~~~
60+
!!! error TS2863: A class cannot extend a primitive type like 'boolean'. Classes can only extend constructable values.
4961

5062
const C12 = class A extends number { }
63+
~~~~~~
64+
!!! error TS2863: A class cannot extend a primitive type like 'number'. Classes can only extend constructable values.
5165
const C13 = class B extends string { }
66+
~~~~~~
67+
!!! error TS2863: A class cannot extend a primitive type like 'string'. Classes can only extend constructable values.
5268
const C14 = class C extends boolean { }
69+
~~~~~~~
70+
!!! error TS2863: A class cannot extend a primitive type like 'boolean'. Classes can only extend constructable values.
5371

testdata/baselines/reference/submodule/conformance/classExtendingPrimitive.errors.txt.diff

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)