fix type checking of variables named "exports" (#52209)

This commit is contained in:
Tobeyyy 2023-01-20 01:59:07 +01:00 committed by GitHub
parent cfe1842adf
commit e01ced050c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 35 additions and 1 deletions

View File

@ -36500,7 +36500,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (checkReferenceExpression(left,
Diagnostics.The_left_hand_side_of_an_assignment_expression_must_be_a_variable_or_a_property_access,
Diagnostics.The_left_hand_side_of_an_assignment_expression_may_not_be_an_optional_property_access)
&& (!isIdentifier(left) || unescapeLeadingUnderscores(left.escapedText) !== "exports")) {
) {
let headMessage: DiagnosticMessage | undefined;
if (exactOptionalPropertyTypes && isPropertyAccessExpression(left) && maybeTypeOfKind(valueType, TypeFlags.Undefined)) {

View File

@ -0,0 +1,8 @@
tests/cases/compiler/typeCheckExportsVariable.ts(2,1): error TS2322: Type 'string' is not assignable to type 'number'.
==== tests/cases/compiler/typeCheckExportsVariable.ts (1 errors) ====
let exports: number;
exports = '';
~~~~~~~
!!! error TS2322: Type 'string' is not assignable to type 'number'.

View File

@ -0,0 +1,7 @@
//// [typeCheckExportsVariable.ts]
let exports: number;
exports = '';
//// [typeCheckExportsVariable.js]
var exports;
exports = '';

View File

@ -0,0 +1,7 @@
=== tests/cases/compiler/typeCheckExportsVariable.ts ===
let exports: number;
>exports : Symbol(exports, Decl(typeCheckExportsVariable.ts, 0, 3))
exports = '';
>exports : Symbol(exports, Decl(typeCheckExportsVariable.ts, 0, 3))

View File

@ -0,0 +1,9 @@
=== tests/cases/compiler/typeCheckExportsVariable.ts ===
let exports: number;
>exports : number
exports = '';
>exports = '' : ""
>exports : number
>'' : ""

View File

@ -0,0 +1,3 @@
let exports: number;
exports = '';