microsoft / microsoft/TypeScript

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

Aperta
#62,519 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Bug Domain: ES Modules Help Wanted
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

Acknowledgement
  • I acknowledge that issues using this template may be closed without further explanation at the maintainer's discretion.
Comment
Search terms
import AND class AND (protected OR private OR modifier)
Description

Given the following CJS module:

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,

import { a, b, c } from "module.cjs";

is permitted, but

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con l’esempio di classe CJS-exported nell’issue e segui il modo in cui TypeScript gestisce NamedImports rispetto a un import-star namespace quando la destinazione è una classe esportata. Riproduci il diverso comportamento di accessibilità, quindi verifica che anche i named imports impongano controlli di accessibilità senza esporre proprietà protected o private.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
38/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.