question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:6
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
wegrycommented, Mar 24, 2020

To get around this for now, I’ve been running yarn tsc --emitDeclarationOnly && yarn rollup -c to get the declaration files without a runtime component and then use this plugin’s output for everything else.

1reaction
slavafomincommented, Mar 25, 2020

@wegry I’ve also been thinking about letting declarations to be generated in a separate process.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TSConfig Reference - Docs on every TSConfig option
This includes generating a type for the import based on the static JSON shape. TypeScript does not support resolving JSON files by default:...
Read more >
Typescript: .d.ts file not recognized - Stack Overflow
We declared "include": ["src"] in our tsconfig.json and this caused declarations.d.ts in root ( "/" ) to not be detected.
Read more >
Understanding TypeScript Configuration Options
If files and include is not provided, TypeScript will automatically compile all files ( .ts, .tsx ) in the directory except for the...
Read more >
ts-node - npm
If no tsconfig.json is loaded from disk, ts-node will use the newest recommended ... Override the module type of certain files, ignoring the ......
Read more >
Typescript does not resolve modules through tsconfig.json's ...
I'd suggest specifying resolution explicitly in your config: ... TS7016: Could not find a declaration file for module 'luciad/view/Map'.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found