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.

Types declared in types.d.ts not included in output of tsc --emitDeclarationOnly

See original GitHub issue

Bug Report

🔎 Search Terms

emitDeclarationOnly allowJs “missing types” “.d.ts file” “declaration file”

🕗 Version & Regression Information

When did you start seeing this bug occur?

Only noticed it just now.

  • This is the behavior in every version I tried (typescript@latest, typescript@next, typescript@4.6), and I reviewed the FAQ for entries about declaration files

⏯ Playground Link

Workbench Repro

💻 Code

// index.js
function bar () {
  return 'baz';
}

/** @returns {BarFn} */
export function foo () {
  return bar;
}

// types.d.ts
type BarFn = () => string;

// jsconfig.json
{
  "compilerOptions": {
    "target": "es2021",
    "module": "es2022",
    "checkJs": true,
    "moduleResolution": "node",
    "strictNullChecks": true
  },
  "include": [
    "index.js",
    "types.d.ts"
  ]
}

// package.json
{
  "name": "tsc-emitdeclarationonly-js",
  "type": "module",
  "main": "index.js",
  "types": "types/lib/index.d.ts",
  "scripts": {
    "gentypes": "tsc -p jsconfig.json --noEmit false --declaration --emitDeclarationOnly --outDir types"
  },
  "devDependencies": {
    "typescript": "^4.9.3"
  }
}

🙁 Actual behavior

The command tsc -p jsconfig.json --noEmit false --declaration --emitDeclarationOnly --outDir types generates this types/index.d.ts:

/** @returns {BarFn} */
export function foo(): BarFn;

which is missing the declaration of BarFn.

🙂 Expected behavior

I expected the BarFn declaration to be included in the generated types/index.d.ts:

/** @returns {BarFn} */
export function foo(): BarFn;
export type BarFn = () => string;

… as indeed it is if I move the declaration from types.d.ts into index.js, expressed in JSDoc form:

/** @typedef {() => string} BarFn */

… but I don’t want it there and in JSDoc form, I want it in a types.d.ts file (visible across my codebase) and in TypeScript form. And included in the generated types/index.d.ts.

Issue Analytics

  • State:open
  • Created 9 months ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
RyanCavanaughcommented, Dec 7, 2022

If you had a .d.ts file which you consumed and you published it along with your package, and someone else did the same thing, then you’d have a conflict in the global scope.

In general if you’re hand-authoring something that’s local to your project, it should be in a .ts file, not a .d.ts file.

0reactions
typescript-botcommented, Jan 3, 2023

👋 Hi, I’m the Repro bot. I can help narrow down and track compiler bugs across releases! This comment reflects the current state of this repro running against the nightly TypeScript.


Comment by @gthb

:+1: Compiled

Historical Information
Version Reproduction Outputs
4.5.2, 4.6.2, 4.7.2, 4.8.2, 4.9.3

:+1: Compiled

</detail>
Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript does not copy d.ts files to build - Stack Overflow
So tsc will not copy your custom types.d.ts file to the output ... The .d.ts files are considered "references" the compiler will not...
Read more >
Documentation - Creating .d.ts Files from .js files - TypeScript
Generate d.ts files. " declaration ": true,. // This compiler run should. // only output d.ts files. " emitDeclarationOnly ": true,. // Types...
Read more >
TypeScript library tips: Rollup your types! | by Martin Hochel
NOTE: this article will not go through how to configure Rollup or how it ... index.js // (type declaration files emitted by tsc)...
Read more >
TypeScript: cannot write file .d.ts because it would overwrite ...
ts files e.g. as part of your include array in tsconfig.json or as part of the tsc arguments, and you're asking TypeScript to...
Read more >
Compiler Options - Microsoft Open Source
emitDeclarationOnly. Default: false. Only emit .d.ts files; do not emit .js files. This setting is useful if you're using a transpiler other than...
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