microsoft / microsoft/TypeScript

Inconsistencies in ESM-style imports of accessibility-modified properties from CJS-exported classes

Offen
#62,519 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bug Domain: ES Modules Help Wanted
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

### Acknowledgement

- [x] I acknowledge that issues using this template may be closed without further explanation at the maintainer's discretion.

### Comment

#### Search terms
```sql
import AND class AND (protected OR private OR modifier)
```

#### Description
Given the following CJS module:

```ts
class X {
public static a = 1;
protected static b = 2;
private static c = 3;
}
export = X;
```

then the behaviour of the static properties when using ESM-style imports is variable. For example,

```ts
import { a, b, c } from "module.cjs";
```
is permitted, but
```ts
import * as m from "module.cjs";
const { a, b, c } = m;
```
is not (`m` is an alias to the CJS export `X`, so this counts as a class property access and is subject to accessibility checks).

Protected and private class properties are also offered by tsserver for Intellisense suggestions when using ESM-style imports, but are obviously hidden when offering properties of the "namespace" object created by `import *` (since it's just an alias to the exported class), and do not appear in its `keyof`.

(The same would also apply if the CJS export were an _instance_ of a class with accessibility-modified prototype properties, but exporting a class constructor is the more likely scenario encountered in the wild.)

Exposing intended-to-be-hidden properties in this way is almost always going to have been unintentional, and the alternative behaviour is fairly straightforward: add accessibility checks for NamedImports elements when the import target is a CJS export.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit dem CJS-exportierten Klassenbeispiel im Issue und verfolge, wie TypeScript NamedImports im Vergleich zu einem import-star namespace behandelt, wenn das Ziel eine exportierte Klasse ist. Reproduziere das unterschiedliche Zugriffsverhalten und überprüfe anschließend, dass named imports ebenfalls Zugriffsprüfungen erzwingen, ohne geschützte oder private Eigenschaften offenzulegen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
38/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.