Repository: TypeScript
Issue: Option to define an allow list for generating declaration/declarationMap
Labels: Suggestion,In Discussion

# Suggestion

<!--
  Please fill in each section completely. Thank you!
-->

## 🔍 Search Terms

<!--
  💡 Did you know? TypeScript has over 2,000 open suggestions!
  🔎 Please search thoroughly before logging new feature requests as most common ideas already have a proposal in progress.
  The "Common Feature Requests" section of the FAQ lists many popular requests: https://github.com/Microsoft/TypeScript/wiki/FAQ#common-feature-requests

  Replace the text below:
-->

build mode, declarations, d.ts

## ✅ Viability Checklist

<!--
   Suggestions that don't meet all these criteria are very, very unlikely to be accepted.
   We always recommend reviewing the TypeScript design goals before investing time writing
   a proposal for ideas outside the scope of the project.
-->
My suggestion meets these guidelines:

* [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
* [x] This wouldn't change the runtime behavior of existing JavaScript code
* [x] This could be implemented without emitting different JS based on the types of the expressions
* [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
* [x] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).


## ⭐ Suggestion

<!-- A summary of what you'd like to see added or changed -->

It would be great tsconfig.json can expose a set of keys to say, "these are the only types that will be exported" from this module, and don't generate .d.ts for the rest.

## 📃 Motivating Example

<!--
  If you were announcing this feature in a blog post, what's a short explanation that shows
  a developer why this feature improves the language?
-->

When building under `--build`, TSC emits `.d.ts` for every single TS file in the module, but intuitively it only needs to
generate declaration files for the ones that's cross module boundary (so that other modules when referencing it),
which is likely a very small subset of types defined in the entire module.

## 💻 Use Cases

<!--
  What do you want to use this for?
  What shortcomings exist with current approaches?
  What workarounds are you using in the meantime?
-->

We have this problem where trying to switch build mode, TSC generates huge type declarations for files who are not
even referenced by other modules at all. The emit time is extremely high and incremental build is barely usable.

Sometimes it's not even possible to build-ify a project, if I have a module that,
```ts
export const thirdPartyLib = await import("3rd-party")
```

If this `thirdPartyLib` export a type that exposes an unimported type in its shape, we'll hit this error

```
error TS4023: Exported variable 'deps' has or is using name 'ElementsProps' from external module "node_modules/@stripe/react-stripe-js/dist/react-stripe" but cannot be named.
```

Concrete example: https://github.com/stripe/react-stripe-js/blob/master/src/components/Elements.tsx#L72