Skip to content

fix: diagnostic message for class name 'Object' when targeting different ECMAScript versions #61850

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46569,7 +46569,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
languageVersion >= ScriptTarget.ES5 && name.escapedText === "Object"
&& host.getEmitModuleFormatOfFile(getSourceFileOfNode(name)) < ModuleKind.ES2015
) {
error(name, Diagnostics.Class_name_cannot_be_Object_when_targeting_ES5_with_module_0, ModuleKind[moduleKind]); // https://github.com/Microsoft/TypeScript/issues/17494
error(name, Diagnostics.Class_name_cannot_be_Object_when_targeting_ES5_and_above_with_module_0, ModuleKind[moduleKind]); // https://github.com/Microsoft/TypeScript/issues/17494
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -3419,7 +3419,7 @@
"category": "Error",
"code": 2724
},
"Class name cannot be 'Object' when targeting ES5 with module {0}.": {
"Class name cannot be 'Object' when targeting ES5 and above with module {0}.": {
"category": "Error",
"code": 2725
},
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/checkForObjectTooStrict.errors.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
checkForObjectTooStrict.ts(3,18): error TS2725: Class name cannot be 'Object' when targeting ES5 with module CommonJS.
checkForObjectTooStrict.ts(3,18): error TS2725: Class name cannot be 'Object' when targeting ES5 and above with module CommonJS.


==== checkForObjectTooStrict.ts (1 errors) ====
module Foo {

export class Object {
~~~~~~
!!! error TS2725: Class name cannot be 'Object' when targeting ES5 with module CommonJS.
!!! error TS2725: Class name cannot be 'Object' when targeting ES5 and above with module CommonJS.

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
exportClassNameWithObjectAMD.ts(1,14): error TS2725: Class name cannot be 'Object' when targeting ES5 with module AMD.
exportClassNameWithObjectAMD.ts(1,14): error TS2725: Class name cannot be 'Object' when targeting ES5 and above with module AMD.


==== exportClassNameWithObjectAMD.ts (1 errors) ====
export class Object {}
~~~~~~
!!! error TS2725: Class name cannot be 'Object' when targeting ES5 with module AMD.
!!! error TS2725: Class name cannot be 'Object' when targeting ES5 and above with module AMD.

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
exportClassNameWithObjectCommonJS.ts(1,14): error TS2725: Class name cannot be 'Object' when targeting ES5 with module CommonJS.
exportClassNameWithObjectCommonJS.ts(1,14): error TS2725: Class name cannot be 'Object' when targeting ES5 and above with module CommonJS.


==== exportClassNameWithObjectCommonJS.ts (1 errors) ====
export class Object {}
~~~~~~
!!! error TS2725: Class name cannot be 'Object' when targeting ES5 with module CommonJS.
!!! error TS2725: Class name cannot be 'Object' when targeting ES5 and above with module CommonJS.

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
exportClassNameWithObjectSystem.ts(1,14): error TS2725: Class name cannot be 'Object' when targeting ES5 with module System.
exportClassNameWithObjectSystem.ts(1,14): error TS2725: Class name cannot be 'Object' when targeting ES5 and above with module System.


==== exportClassNameWithObjectSystem.ts (1 errors) ====
export class Object {}
~~~~~~
!!! error TS2725: Class name cannot be 'Object' when targeting ES5 with module System.
!!! error TS2725: Class name cannot be 'Object' when targeting ES5 and above with module System.

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
exportClassNameWithObjectUMD.ts(1,14): error TS2725: Class name cannot be 'Object' when targeting ES5 with module UMD.
exportClassNameWithObjectUMD.ts(1,14): error TS2725: Class name cannot be 'Object' when targeting ES5 and above with module UMD.


==== exportClassNameWithObjectUMD.ts (1 errors) ====
export class Object {}
~~~~~~
!!! error TS2725: Class name cannot be 'Object' when targeting ES5 with module UMD.
!!! error TS2725: Class name cannot be 'Object' when targeting ES5 and above with module UMD.

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
exportDefaultClassNameWithObject.ts(1,22): error TS2725: Class name cannot be 'Object' when targeting ES5 with module CommonJS.
exportDefaultClassNameWithObject.ts(1,22): error TS2725: Class name cannot be 'Object' when targeting ES5 and above with module CommonJS.


==== exportDefaultClassNameWithObject.ts (1 errors) ====
export default class Object {}
~~~~~~
!!! error TS2725: Class name cannot be 'Object' when targeting ES5 with module CommonJS.
!!! error TS2725: Class name cannot be 'Object' when targeting ES5 and above with module CommonJS.

4 changes: 2 additions & 2 deletions tests/baselines/reference/instanceofOperator.errors.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
instanceofOperator.ts(7,11): error TS2725: Class name cannot be 'Object' when targeting ES5 with module CommonJS.
instanceofOperator.ts(7,11): error TS2725: Class name cannot be 'Object' when targeting ES5 and above with module CommonJS.
instanceofOperator.ts(12,5): error TS2358: The left-hand side of an 'instanceof' expression must be of type 'any', an object type or a type parameter.
instanceofOperator.ts(15,20): error TS2359: The right-hand side of an 'instanceof' expression must be either of type 'any', a class, function, or other type assignable to the 'Function' interface type, or an object type with a 'Symbol.hasInstance' method.
instanceofOperator.ts(16,23): error TS2359: The right-hand side of an 'instanceof' expression must be either of type 'any', a class, function, or other type assignable to the 'Function' interface type, or an object type with a 'Symbol.hasInstance' method.
Expand All @@ -15,7 +15,7 @@ instanceofOperator.ts(21,5): error TS2358: The left-hand side of an 'instanceof'
module test {
class Object { }
~~~~~~
!!! error TS2725: Class name cannot be 'Object' when targeting ES5 with module CommonJS.
!!! error TS2725: Class name cannot be 'Object' when targeting ES5 and above with module CommonJS.
var obj: Object;


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
subfolder/index.js(2,10): error TS2441: Duplicate identifier 'require'. Compiler reserves name 'require' in top level scope of a module.
subfolder/index.js(3,7): error TS2441: Duplicate identifier 'exports'. Compiler reserves name 'exports' in top level scope of a module.
subfolder/index.js(4,7): error TS2725: Class name cannot be 'Object' when targeting ES5 with module Node16.
subfolder/index.js(4,7): error TS2725: Class name cannot be 'Object' when targeting ES5 and above with module Node16.
subfolder/index.js(5,14): error TS1216: Identifier expected. '__esModule' is reserved as an exported marker when transforming ECMAScript modules.


Expand All @@ -14,7 +14,7 @@ subfolder/index.js(5,14): error TS1216: Identifier expected. '__esModule' is res
!!! error TS2441: Duplicate identifier 'exports'. Compiler reserves name 'exports' in top level scope of a module.
class Object {}
~~~~~~
!!! error TS2725: Class name cannot be 'Object' when targeting ES5 with module Node16.
!!! error TS2725: Class name cannot be 'Object' when targeting ES5 and above with module Node16.
export const __esModule = false;
~~~~~~~~~~
!!! error TS1216: Identifier expected. '__esModule' is reserved as an exported marker when transforming ECMAScript modules.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
subfolder/index.js(2,10): error TS2441: Duplicate identifier 'require'. Compiler reserves name 'require' in top level scope of a module.
subfolder/index.js(3,7): error TS2441: Duplicate identifier 'exports'. Compiler reserves name 'exports' in top level scope of a module.
subfolder/index.js(4,7): error TS2725: Class name cannot be 'Object' when targeting ES5 with module Node18.
subfolder/index.js(4,7): error TS2725: Class name cannot be 'Object' when targeting ES5 and above with module Node18.
subfolder/index.js(5,14): error TS1216: Identifier expected. '__esModule' is reserved as an exported marker when transforming ECMAScript modules.


Expand All @@ -14,7 +14,7 @@ subfolder/index.js(5,14): error TS1216: Identifier expected. '__esModule' is res
!!! error TS2441: Duplicate identifier 'exports'. Compiler reserves name 'exports' in top level scope of a module.
class Object {}
~~~~~~
!!! error TS2725: Class name cannot be 'Object' when targeting ES5 with module Node18.
!!! error TS2725: Class name cannot be 'Object' when targeting ES5 and above with module Node18.
export const __esModule = false;
~~~~~~~~~~
!!! error TS1216: Identifier expected. '__esModule' is reserved as an exported marker when transforming ECMAScript modules.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
subfolder/index.js(2,10): error TS2441: Duplicate identifier 'require'. Compiler reserves name 'require' in top level scope of a module.
subfolder/index.js(3,7): error TS2441: Duplicate identifier 'exports'. Compiler reserves name 'exports' in top level scope of a module.
subfolder/index.js(4,7): error TS2725: Class name cannot be 'Object' when targeting ES5 with module Node20.
subfolder/index.js(4,7): error TS2725: Class name cannot be 'Object' when targeting ES5 and above with module Node20.
subfolder/index.js(5,14): error TS1216: Identifier expected. '__esModule' is reserved as an exported marker when transforming ECMAScript modules.


Expand All @@ -14,7 +14,7 @@ subfolder/index.js(5,14): error TS1216: Identifier expected. '__esModule' is res
!!! error TS2441: Duplicate identifier 'exports'. Compiler reserves name 'exports' in top level scope of a module.
class Object {}
~~~~~~
!!! error TS2725: Class name cannot be 'Object' when targeting ES5 with module Node20.
!!! error TS2725: Class name cannot be 'Object' when targeting ES5 and above with module Node20.
export const __esModule = false;
~~~~~~~~~~
!!! error TS1216: Identifier expected. '__esModule' is reserved as an exported marker when transforming ECMAScript modules.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
subfolder/index.js(2,10): error TS2441: Duplicate identifier 'require'. Compiler reserves name 'require' in top level scope of a module.
subfolder/index.js(3,7): error TS2441: Duplicate identifier 'exports'. Compiler reserves name 'exports' in top level scope of a module.
subfolder/index.js(4,7): error TS2725: Class name cannot be 'Object' when targeting ES5 with module NodeNext.
subfolder/index.js(4,7): error TS2725: Class name cannot be 'Object' when targeting ES5 and above with module NodeNext.
subfolder/index.js(5,14): error TS1216: Identifier expected. '__esModule' is reserved as an exported marker when transforming ECMAScript modules.


Expand All @@ -14,7 +14,7 @@ subfolder/index.js(5,14): error TS1216: Identifier expected. '__esModule' is res
!!! error TS2441: Duplicate identifier 'exports'. Compiler reserves name 'exports' in top level scope of a module.
class Object {}
~~~~~~
!!! error TS2725: Class name cannot be 'Object' when targeting ES5 with module NodeNext.
!!! error TS2725: Class name cannot be 'Object' when targeting ES5 and above with module NodeNext.
export const __esModule = false;
~~~~~~~~~~
!!! error TS1216: Identifier expected. '__esModule' is reserved as an exported marker when transforming ECMAScript modules.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
subfolder/index.ts(2,10): error TS2441: Duplicate identifier 'require'. Compiler reserves name 'require' in top level scope of a module.
subfolder/index.ts(3,7): error TS2441: Duplicate identifier 'exports'. Compiler reserves name 'exports' in top level scope of a module.
subfolder/index.ts(4,7): error TS2725: Class name cannot be 'Object' when targeting ES5 with module Node16.
subfolder/index.ts(4,7): error TS2725: Class name cannot be 'Object' when targeting ES5 and above with module Node16.
subfolder/index.ts(5,14): error TS1216: Identifier expected. '__esModule' is reserved as an exported marker when transforming ECMAScript modules.


Expand All @@ -14,7 +14,7 @@ subfolder/index.ts(5,14): error TS1216: Identifier expected. '__esModule' is res
!!! error TS2441: Duplicate identifier 'exports'. Compiler reserves name 'exports' in top level scope of a module.
class Object {}
~~~~~~
!!! error TS2725: Class name cannot be 'Object' when targeting ES5 with module Node16.
!!! error TS2725: Class name cannot be 'Object' when targeting ES5 and above with module Node16.
export const __esModule = false;
~~~~~~~~~~
!!! error TS1216: Identifier expected. '__esModule' is reserved as an exported marker when transforming ECMAScript modules.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
subfolder/index.ts(2,10): error TS2441: Duplicate identifier 'require'. Compiler reserves name 'require' in top level scope of a module.
subfolder/index.ts(3,7): error TS2441: Duplicate identifier 'exports'. Compiler reserves name 'exports' in top level scope of a module.
subfolder/index.ts(4,7): error TS2725: Class name cannot be 'Object' when targeting ES5 with module Node18.
subfolder/index.ts(4,7): error TS2725: Class name cannot be 'Object' when targeting ES5 and above with module Node18.
subfolder/index.ts(5,14): error TS1216: Identifier expected. '__esModule' is reserved as an exported marker when transforming ECMAScript modules.


Expand All @@ -14,7 +14,7 @@ subfolder/index.ts(5,14): error TS1216: Identifier expected. '__esModule' is res
!!! error TS2441: Duplicate identifier 'exports'. Compiler reserves name 'exports' in top level scope of a module.
class Object {}
~~~~~~
!!! error TS2725: Class name cannot be 'Object' when targeting ES5 with module Node18.
!!! error TS2725: Class name cannot be 'Object' when targeting ES5 and above with module Node18.
export const __esModule = false;
~~~~~~~~~~
!!! error TS1216: Identifier expected. '__esModule' is reserved as an exported marker when transforming ECMAScript modules.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
subfolder/index.ts(2,10): error TS2441: Duplicate identifier 'require'. Compiler reserves name 'require' in top level scope of a module.
subfolder/index.ts(3,7): error TS2441: Duplicate identifier 'exports'. Compiler reserves name 'exports' in top level scope of a module.
subfolder/index.ts(4,7): error TS2725: Class name cannot be 'Object' when targeting ES5 with module Node20.
subfolder/index.ts(4,7): error TS2725: Class name cannot be 'Object' when targeting ES5 and above with module Node20.
subfolder/index.ts(5,14): error TS1216: Identifier expected. '__esModule' is reserved as an exported marker when transforming ECMAScript modules.


Expand All @@ -14,7 +14,7 @@ subfolder/index.ts(5,14): error TS1216: Identifier expected. '__esModule' is res
!!! error TS2441: Duplicate identifier 'exports'. Compiler reserves name 'exports' in top level scope of a module.
class Object {}
~~~~~~
!!! error TS2725: Class name cannot be 'Object' when targeting ES5 with module Node20.
!!! error TS2725: Class name cannot be 'Object' when targeting ES5 and above with module Node20.
export const __esModule = false;
~~~~~~~~~~
!!! error TS1216: Identifier expected. '__esModule' is reserved as an exported marker when transforming ECMAScript modules.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
subfolder/index.ts(2,10): error TS2441: Duplicate identifier 'require'. Compiler reserves name 'require' in top level scope of a module.
subfolder/index.ts(3,7): error TS2441: Duplicate identifier 'exports'. Compiler reserves name 'exports' in top level scope of a module.
subfolder/index.ts(4,7): error TS2725: Class name cannot be 'Object' when targeting ES5 with module NodeNext.
subfolder/index.ts(4,7): error TS2725: Class name cannot be 'Object' when targeting ES5 and above with module NodeNext.
subfolder/index.ts(5,14): error TS1216: Identifier expected. '__esModule' is reserved as an exported marker when transforming ECMAScript modules.


Expand All @@ -14,7 +14,7 @@ subfolder/index.ts(5,14): error TS1216: Identifier expected. '__esModule' is res
!!! error TS2441: Duplicate identifier 'exports'. Compiler reserves name 'exports' in top level scope of a module.
class Object {}
~~~~~~
!!! error TS2725: Class name cannot be 'Object' when targeting ES5 with module NodeNext.
!!! error TS2725: Class name cannot be 'Object' when targeting ES5 and above with module NodeNext.
export const __esModule = false;
~~~~~~~~~~
!!! error TS1216: Identifier expected. '__esModule' is reserved as an exported marker when transforming ECMAScript modules.
Expand Down