microsoft / microsoft/TypeScript

Add option to detect and strip internal exports

Aperta
#58,250 0 commenti 3 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

🔍 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.

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 esaminando l’output delle dichiarazioni per il campo exports di package.json, le compilerOptions di tsconfig.json e il comportamento esistente di stripInternal descritto nell’issue. Confronta le dichiarazioni emesse per src/index.ts e src/internal.ts con l’output proposto. Il lavoro è completato quando un’opzione può rilevare i tipi non necessari all’interfaccia pubblica e ometterli senza modificare il JavaScript a runtime.

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

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.