Skip to content

Declarations not generated for type-only files not explicitly specified in tsconfig #211

@slavafomin

Description

@slavafomin

Hello, again!

I've noticed the following behavior: TypeScript declarations are not generated for source modules from which only types are imported and which are not explicitly added to the project using files or include options in tsconfig.json.

Project configuration

// tsconfig.json
{
  "compilerOptions": {
    "outDir": "dist/",
    "declarationDir": "dist/types/",
    "declaration": true
  },
  
  // specifying only entry-point
  "files": ["src/index.ts"]
}
// rollup.config.js

import typeScriptPlugin2 from 'rollup-plugin-typescript2';

export default {
  input: 'src/index.ts',
  output: {
    file: 'dist/index.js',
    format: 'esm',
  },
  plugins: [
    typeScriptPlugin2({
      verbosity: 2,
      clean: true,
      useTsconfigDeclarationDir: true,
    }),
  ],
};

Source files

// src/index.ts

// importing only type symbol from './foo.ts'
import { Foo } from './foo';

export class Main {
  constructor(foo: Foo) {
    console.log(foo);
  }
}
// src/foo.ts

// type symbol
export type Foo = 'foo';

// constant symbol (not imported)
export const defaultFoo: Foo = 'foo';
// src/unused.ts

export type Unused = 'unused';

Generated files

// dist/index.js

var Main = /** @class */ (function () {
    function Main(foo) {
        console.log(foo);
    }
    return Main;
}());

export { Main };
// dist/types/index.d.ts

// Error: TS2307: Cannot find module './foo'.
import { Foo } from './foo';

export declare class Main {
    constructor(foo: Foo);
}
  • the dist/types/unused.d.ts is not generated, which is GOOD.
  • however, the dist/types/foo.d.ts is not generated, which is BAD.

And if I would set "include": ["src/"], the dist/types/unused.d.ts would be generated, however, it shouldn't, because it's not imported anywhere.


Our goal here is to specify only the entry point of our project and let the compiler to handle only source files that are actually used in the project and avoid processing, generating declarations and reporting errors for files that are not used (redundant).

Metadata

Metadata

Assignees

Labels

kind: bugSomething isn't working properlytopic: type-only / emit-less importsRelated to importing type-only files that will not be emitted

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions