Skip to content

Commit e01ced0

Browse files
fix type checking of variables named "exports" (microsoft#52209)
1 parent cfe1842 commit e01ced0

6 files changed

+35
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36500,7 +36500,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3650036500
if (checkReferenceExpression(left,
3650136501
Diagnostics.The_left_hand_side_of_an_assignment_expression_must_be_a_variable_or_a_property_access,
3650236502
Diagnostics.The_left_hand_side_of_an_assignment_expression_may_not_be_an_optional_property_access)
36503-
&& (!isIdentifier(left) || unescapeLeadingUnderscores(left.escapedText) !== "exports")) {
36503+
) {
3650436504

3650536505
let headMessage: DiagnosticMessage | undefined;
3650636506
if (exactOptionalPropertyTypes && isPropertyAccessExpression(left) && maybeTypeOfKind(valueType, TypeFlags.Undefined)) {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
tests/cases/compiler/typeCheckExportsVariable.ts(2,1): error TS2322: Type 'string' is not assignable to type 'number'.
2+
3+
4+
==== tests/cases/compiler/typeCheckExportsVariable.ts (1 errors) ====
5+
let exports: number;
6+
exports = '';
7+
~~~~~~~
8+
!!! error TS2322: Type 'string' is not assignable to type 'number'.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//// [typeCheckExportsVariable.ts]
2+
let exports: number;
3+
exports = '';
4+
5+
//// [typeCheckExportsVariable.js]
6+
var exports;
7+
exports = '';
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/compiler/typeCheckExportsVariable.ts ===
2+
let exports: number;
3+
>exports : Symbol(exports, Decl(typeCheckExportsVariable.ts, 0, 3))
4+
5+
exports = '';
6+
>exports : Symbol(exports, Decl(typeCheckExportsVariable.ts, 0, 3))
7+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
=== tests/cases/compiler/typeCheckExportsVariable.ts ===
2+
let exports: number;
3+
>exports : number
4+
5+
exports = '';
6+
>exports = '' : ""
7+
>exports : number
8+
>'' : ""
9+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
let exports: number;
3+
exports = '';

0 commit comments

Comments
 (0)