Exclude d.ts file from being generated

Viewed 39

I got a util file which should not be exported as d.ts file as it is for internal use only. The file looks like this

// util.ts

/** @internal */
export const fun = function (...) {
   ...
};

/** @internal */
export const arrowFun = (...) => {
   ...
} 

The issue is that with declaration: true I still get the util.d.ts file even if it looks like this

// util.d.ts
export {};

The tsconfig.json is

{
  "compilerOptions": {
    "target": "es2021",
    "module": "commonjs",
    "outDir": "dist",
    "strict": true,
    "allowSyntheticDefaultImports": true,
    "declaration": true,
    "stripInternal": true
  },
  "include": [
    "src/index.ts"
  ],
  "exclude": [
    "src/util.ts"
  ]
}

Is there a way how not to generate the file at all?

Please do not answer with .gitignore or .npmrc stuff. I want to keep this clear and easy to read without having people to search around why the file is not exported

1 Answers

Per the typescript docs exclude only modifies the behaviour of include but does not explicitly exclude a listed file from being included.

Important: exclude only changes which files are included as a result of the include setting. A file specified by exclude can still become part of your codebase due to an import statement in your code, a types inclusion, a /// <reference directive, or being specified in the files list.

It is not a mechanism that prevents a file from being included in the codebase - it simply changes what the include setting finds.

Regardless of any compiler option, if any explicitly included file imports or uses a /// <reference to another file in your code all transitive files will be included in the final output.

Thus, it appears the only way to remove this would be some sort of manual postbuild script.

Related