Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to detect and strip internal exports #58250

Open
6 tasks done
remcohaszing opened this issue Apr 19, 2024 · 0 comments
Open
6 tasks done

Add option to detect and strip internal exports #58250

remcohaszing opened this issue Apr 19, 2024 · 0 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@remcohaszing
Copy link

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

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Apr 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants