Skip to content
Closed
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
7 changes: 6 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4069,14 +4069,19 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {

let symbolFromVariable: Symbol | undefined;
// First check if module was specified with "export=". If so, get the member from the resolved type
if (moduleSymbol && moduleSymbol.exports && moduleSymbol.exports.get(InternalSymbolName.ExportEquals)) {
const isExportEquals = !!(moduleSymbol && moduleSymbol.exports && moduleSymbol.exports.get(InternalSymbolName.ExportEquals));
if (isExportEquals) {
symbolFromVariable = getPropertyOfType(getTypeOfSymbol(targetSymbol), nameText, /*skipObjectFunctionPropertyAugment*/ true);
}
else {
symbolFromVariable = getPropertyOfVariable(targetSymbol, nameText);
}
// if symbolFromVariable is export - get its final target
symbolFromVariable = resolveSymbol(symbolFromVariable, dontResolveAlias);
// Check accessibility for named imports from CJS export= modules whose exports are class members
if (!dontResolveAlias && isExportEquals && symbolFromVariable && isImportSpecifier(specifier)) {
checkPropertyAccessibilityAtLocation(specifier, /*isSuper*/ false, /*writing*/ false, getTypeOfSymbol(targetSymbol), symbolFromVariable, name);
}

let symbolFromModule = getExportOfModule(targetSymbol, nameText, specifier, dontResolveAlias);
if (symbolFromModule === undefined && nameText === InternalSymbolName.Default) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
main.ts(1,13): error TS2445: Property 'b' is protected and only accessible within class 'X' and its subclasses.
main.ts(1,16): error TS2341: Property 'c' is private and only accessible within class 'X'.
main.ts(1,25): error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export.


==== module.ts (0 errors) ====
class X {
public static a = 1;
protected static b = 2;
private static c = 3;
}
export = X;

==== main.ts (3 errors) ====
import { a, b, c } from "./module";
~
!!! error TS2445: Property 'b' is protected and only accessible within class 'X' and its subclasses.
~
!!! error TS2341: Property 'c' is private and only accessible within class 'X'.
~~~~~~~~~~
!!! error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//// [tests/cases/compiler/namedImportFromCjsClassExportAccessibility.ts] ////

//// [module.ts]
class X {
public static a = 1;
protected static b = 2;
private static c = 3;
}
export = X;

//// [main.ts]
import { a, b, c } from "./module";


//// [module.js]
"use strict";
class X {
}
X.a = 1;
X.b = 2;
X.c = 3;
module.exports = X;
//// [main.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//// [tests/cases/compiler/namedImportFromCjsClassExportAccessibility.ts] ////

=== module.ts ===
class X {
>X : Symbol(X, Decl(module.ts, 0, 0))

public static a = 1;
>a : Symbol(X.a, Decl(module.ts, 0, 9))

protected static b = 2;
>b : Symbol(X.b, Decl(module.ts, 1, 24))

private static c = 3;
>c : Symbol(X.c, Decl(module.ts, 2, 27))
}
export = X;
>X : Symbol(X, Decl(module.ts, 0, 0))

=== main.ts ===
import { a, b, c } from "./module";
>a : Symbol(a, Decl(main.ts, 0, 8))
>b : Symbol(b, Decl(main.ts, 0, 11))
>c : Symbol(c, Decl(main.ts, 0, 14))

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//// [tests/cases/compiler/namedImportFromCjsClassExportAccessibility.ts] ////

=== module.ts ===
class X {
>X : X
> : ^

public static a = 1;
>a : number
> : ^^^^^^
>1 : 1
> : ^

protected static b = 2;
>b : number
> : ^^^^^^
>2 : 2
> : ^

private static c = 3;
>c : number
> : ^^^^^^
>3 : 3
> : ^
}
export = X;
>X : X
> : ^

=== main.ts ===
import { a, b, c } from "./module";
>a : number
> : ^^^^^^
>b : number
> : ^^^^^^
>c : number
> : ^^^^^^

15 changes: 15 additions & 0 deletions tests/cases/compiler/namedImportFromCjsClassExportAccessibility.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// @target: es2020
// @module: commonjs
// @strict: true
// @esModuleInterop: true

// @Filename: module.ts
class X {
public static a = 1;
protected static b = 2;
private static c = 3;
}
export = X;

// @Filename: main.ts
import { a, b, c } from "./module";