microsoft / microsoft/TypeScript

Add option to detect and strip internal exports

Offen
#58,250 0 Kommentare 3 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

🔍 Search Terms
  • “detect internal”
  • “stripInternal”
✅ Viability Checklist
⭐ Suggestion

I would like TypeScript to be able to detect unused exports and strip them. Most packages define and import and export types that are for internal use only. If package.json has the exports field, TypeScript knows which parts are part of the public interface.

📃 Motivating Example

Let’s say you have the following files:

// package.json
{
  "exports": "./dist/index.js"
}
// tsconfig.json
{
  "compilerOptions": {
    "outDir": "dist",
    "rootDir": "src"
  }
}
// src/index.ts
import { internalFunction, PublicType } from './internal.js'

export { PublicType }

export function publicFunction(): PublicType {
  return internalFunction({ internal: true })
}
// src/internal.ts
export interface PublicType {
  public: boolean
}

export interface InternalType {
  internal: boolean
}

export function internalFuncion(argument: InternalType): PublicType {
  return { public: true }
}

TypeScript emits the following type definitions:

// dist/index.d.ts
import { PublicType } from './internal.js'

export { PublicType }

export function publicFunction(): PublicType;
// dist/internal.d.ts
export interface PublicType {
  public: boolean
}

export interface InternalType {
  internal: boolean
}

export function internalFuncion(argument: InternalType): PublicType;

With the option detectInternal, TypeScript omits types that aren’t needed for the public interface. Instead of the above, TypeScript would emit:

// dist/index.d.ts
import { PublicType } from './internal.js'

export { PublicType }

export function publicFunction(): PublicType;
// dist/internal.d.ts
export interface PublicType {
  public: boolean
}
💻 Use Cases

I want to use this option for libraries. Libraries often have such internal exports that are not relevant to the user. These often include types that are imported from other libraries. If TypeScript strips unused exports while compiling a library, it has to load fewer files and process fewer types when a user consumes this library.

Currently a library author can use the stripInternal option and @internal JSDoc tags. This means the author has to manually find and tag internals. This is error prone. It can lead to both false positive and false negative internal markings, without TypeScript complaining.

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 der Überprüfung der Deklarationsausgabe für das exports-Feld von package.json, die compilerOptions von tsconfig.json und das im Issue beschriebene bestehende stripInternal-Verhalten. Vergleiche die erzeugten Deklarationen für src/index.ts und src/internal.ts mit der vorgeschlagenen Ausgabe. Als abgeschlossen gilt die Arbeit, wenn eine Option Typen erkennen kann, die für die öffentliche Schnittstelle nicht benötigt werden, und sie weglassen kann, ohne das Laufzeit-JavaScript zu ändern.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

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