Skip to content

Commit

Permalink
fix(60484): report error when implementing a prim type in a class exp…
Browse files Browse the repository at this point in the history
…ression
  • Loading branch information
a-tarasyuk committed Nov 12, 2024
1 parent b58ac4a commit 455d591
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3458,10 +3458,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (containerKind === SyntaxKind.InterfaceDeclaration && heritageKind === SyntaxKind.ExtendsKeyword) {
error(errorLocation, Diagnostics.An_interface_cannot_extend_a_primitive_type_like_0_It_can_only_extend_other_named_object_types, unescapeLeadingUnderscores(name));
}
else if (containerKind === SyntaxKind.ClassDeclaration && heritageKind === SyntaxKind.ExtendsKeyword) {
else if (isClassLike(grandparent.parent) && heritageKind === SyntaxKind.ExtendsKeyword) {
error(errorLocation, Diagnostics.A_class_cannot_extend_a_primitive_type_like_0_Classes_can_only_extend_constructable_values, unescapeLeadingUnderscores(name));
}
else if (containerKind === SyntaxKind.ClassDeclaration && heritageKind === SyntaxKind.ImplementsKeyword) {
else if (isClassLike(grandparent.parent) && heritageKind === SyntaxKind.ImplementsKeyword) {
error(errorLocation, Diagnostics.A_class_cannot_implement_a_primitive_type_like_0_It_can_only_implement_other_named_object_types, unescapeLeadingUnderscores(name));
}
}
Expand Down
31 changes: 29 additions & 2 deletions tests/baselines/reference/classImplementsPrimitive.errors.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
classImplementsPrimitive.ts(3,20): error TS2864: A class cannot implement a primitive type like 'number'. It can only implement other named object types.
classImplementsPrimitive.ts(4,21): error TS2864: A class cannot implement a primitive type like 'string'. It can only implement other named object types.
classImplementsPrimitive.ts(5,21): error TS2864: A class cannot implement a primitive type like 'boolean'. It can only implement other named object types.
classImplementsPrimitive.ts(7,29): error TS2864: A class cannot implement a primitive type like 'number'. It can only implement other named object types.
classImplementsPrimitive.ts(8,29): error TS2864: A class cannot implement a primitive type like 'string'. It can only implement other named object types.
classImplementsPrimitive.ts(9,29): error TS2864: A class cannot implement a primitive type like 'boolean'. It can only implement other named object types.
classImplementsPrimitive.ts(11,31): error TS2864: A class cannot implement a primitive type like 'number'. It can only implement other named object types.
classImplementsPrimitive.ts(12,31): error TS2864: A class cannot implement a primitive type like 'string'. It can only implement other named object types.
classImplementsPrimitive.ts(13,31): error TS2864: A class cannot implement a primitive type like 'boolean'. It can only implement other named object types.


==== classImplementsPrimitive.ts (3 errors) ====
==== classImplementsPrimitive.ts (9 errors) ====
// classes cannot implement primitives

class C implements number { }
Expand All @@ -14,4 +20,25 @@ classImplementsPrimitive.ts(5,21): error TS2864: A class cannot implement a prim
!!! error TS2864: A class cannot implement a primitive type like 'string'. It can only implement other named object types.
class C3 implements boolean { }
~~~~~~~
!!! error TS2864: A class cannot implement a primitive type like 'boolean'. It can only implement other named object types.
!!! error TS2864: A class cannot implement a primitive type like 'boolean'. It can only implement other named object types.

const C4 = class implements number {}
~~~~~~
!!! error TS2864: A class cannot implement a primitive type like 'number'. It can only implement other named object types.
const C5 = class implements string {}
~~~~~~
!!! error TS2864: A class cannot implement a primitive type like 'string'. It can only implement other named object types.
const C6 = class implements boolean {}
~~~~~~~
!!! error TS2864: A class cannot implement a primitive type like 'boolean'. It can only implement other named object types.

const C7 = class A implements number { }
~~~~~~
!!! error TS2864: A class cannot implement a primitive type like 'number'. It can only implement other named object types.
const C8 = class B implements string { }
~~~~~~
!!! error TS2864: A class cannot implement a primitive type like 'string'. It can only implement other named object types.
const C9 = class C implements boolean { }
~~~~~~~
!!! error TS2864: A class cannot implement a primitive type like 'boolean'. It can only implement other named object types.

41 changes: 40 additions & 1 deletion tests/baselines/reference/classImplementsPrimitive.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@

class C implements number { }
class C2 implements string { }
class C3 implements boolean { }
class C3 implements boolean { }

const C4 = class implements number {}
const C5 = class implements string {}
const C6 = class implements boolean {}

const C7 = class A implements number { }
const C8 = class B implements string { }
const C9 = class C implements boolean { }


//// [classImplementsPrimitive.js]
// classes cannot implement primitives
Expand All @@ -24,3 +33,33 @@ var C3 = /** @class */ (function () {
}
return C3;
}());
var C4 = /** @class */ (function () {
function class_1() {
}
return class_1;
}());
var C5 = /** @class */ (function () {
function class_2() {
}
return class_2;
}());
var C6 = /** @class */ (function () {
function class_3() {
}
return class_3;
}());
var C7 = /** @class */ (function () {
function A() {
}
return A;
}());
var C8 = /** @class */ (function () {
function B() {
}
return B;
}());
var C9 = /** @class */ (function () {
function C() {
}
return C;
}());
21 changes: 21 additions & 0 deletions tests/baselines/reference/classImplementsPrimitive.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,24 @@ class C2 implements string { }
class C3 implements boolean { }
>C3 : Symbol(C3, Decl(classImplementsPrimitive.ts, 3, 30))

const C4 = class implements number {}
>C4 : Symbol(C4, Decl(classImplementsPrimitive.ts, 6, 5))

const C5 = class implements string {}
>C5 : Symbol(C5, Decl(classImplementsPrimitive.ts, 7, 5))

const C6 = class implements boolean {}
>C6 : Symbol(C6, Decl(classImplementsPrimitive.ts, 8, 5))

const C7 = class A implements number { }
>C7 : Symbol(C7, Decl(classImplementsPrimitive.ts, 10, 5))
>A : Symbol(A, Decl(classImplementsPrimitive.ts, 10, 10))

const C8 = class B implements string { }
>C8 : Symbol(C8, Decl(classImplementsPrimitive.ts, 11, 5))
>B : Symbol(B, Decl(classImplementsPrimitive.ts, 11, 10))

const C9 = class C implements boolean { }
>C9 : Symbol(C9, Decl(classImplementsPrimitive.ts, 12, 5))
>C : Symbol(C, Decl(classImplementsPrimitive.ts, 12, 10))

42 changes: 42 additions & 0 deletions tests/baselines/reference/classImplementsPrimitive.types
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,45 @@ class C3 implements boolean { }
>C3 : C3
> : ^^

const C4 = class implements number {}
>C4 : typeof C4
> : ^^^^^^^^^
>class implements number {} : typeof C4
> : ^^^^^^^^^

const C5 = class implements string {}
>C5 : typeof C5
> : ^^^^^^^^^
>class implements string {} : typeof C5
> : ^^^^^^^^^

const C6 = class implements boolean {}
>C6 : typeof C6
> : ^^^^^^^^^
>class implements boolean {} : typeof C6
> : ^^^^^^^^^

const C7 = class A implements number { }
>C7 : typeof A
> : ^^^^^^^^
>class A implements number { } : typeof A
> : ^^^^^^^^
>A : typeof A
> : ^^^^^^^^

const C8 = class B implements string { }
>C8 : typeof B
> : ^^^^^^^^
>class B implements string { } : typeof B
> : ^^^^^^^^
>B : typeof B
> : ^^^^^^^^

const C9 = class C implements boolean { }
>C9 : typeof C
> : ^^^^^^^^
>class C implements boolean { } : typeof C
> : ^^^^^^^^
>C : typeof C
> : ^^^^^^^^

10 changes: 9 additions & 1 deletion tests/cases/compiler/classImplementsPrimitive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@

class C implements number { }
class C2 implements string { }
class C3 implements boolean { }
class C3 implements boolean { }

const C4 = class implements number {}
const C5 = class implements string {}
const C6 = class implements boolean {}

const C7 = class A implements number { }
const C8 = class B implements string { }
const C9 = class C implements boolean { }

0 comments on commit 455d591

Please sign in to comment.