Skip to content

bug(typescript/declarations): only generating type declaration files for source file specified in package.json #826

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sno2 opened this issue Mar 30, 2021 · 4 comments

Comments

@sno2
Copy link

sno2 commented Mar 30, 2021

When following the starter guide and the using with typescript section in the README, the only type declaration file (d.ts) generated is the one specified in package.json#source. For example, let's say we have the following files in our src directory:

// index.ts
import type { Name } from "./types";
export const name: Name = "Bill";

// types.ts
export type Name = "Bill" | "Joe";

When I run npm run build, it only generates a single declaration file within the dist folder at the location specified in package.json#types. This does not work because the single declaration file includes imports to other declarations for types.d.ts which does not exist.

// dist/index.d.ts
import type { Name } from "./types"; // no `types.d.ts` in dist so it just says `any` and breaks all other types!
export declare const name: Name;

The current work-around solution I have came up with is to just add another script to the package.json to generate the correct declaration files. Sadly, this does not make it automatically happen when saving files in microbundle, though.

{
  "name": "foo",
  "version": "0.0.1",
  "source": "src/index.ts",
  "types": "dist/index.d.ts",
  "main": "dist/index.js",
  "exports": "dist/index.modern.js",
  "unpkg": "dist/index.umd.js",
  "jsdelivr": "dist/index.umd.js",
  "scripts": {
    "build": "npm run build:js && npm run build:types",
    "build:js": "microbundle",
    "build:types": "tsc --emitDeclarationOnly",
    "dev": "microbundle watch",
  }
}

My proposal for a solution would be to just read the directory that the source file is located in within the package.json file and emit all types within there as it seems like we're only generating the single types for the source file.

Edit: Sorry, I forgot to thank you for this amazing library! It's really been a complete joy working with until this nasty issue popped up.

@rschristian
Copy link
Collaborator

Should work out-of-the-box, might be something up with your tsconfig.json.

Here's a demo repository, feel free to take a look and see if you've done something differently.

@sno2
Copy link
Author

sno2 commented Mar 30, 2021

Should work out-of-the-box, might be something up with your tsconfig.json.

Here's a demo repository, feel free to take a look and see if you've done something differently.

Thank you very much for your quick response. It is working perfectly now. I'm very confused, because I can't reproduce the behavior now and I spent a long time yesterday trying to get it to work yesterday. Sorry for wasting your time.

@sno2 sno2 closed this as completed Mar 30, 2021
@rschristian
Copy link
Collaborator

Certainly no worries! Happy to help, and glad it's working for you. Let me know if you end up reproducing it again and we can take a look.

@agilgur5
Copy link
Contributor

Thought I'd update here that I fixed the root cause upstream in ezolenko/rollup-plugin-typescript2#406, which was released in rpt2 0.34.0.

The tsconfig include workaround should no longer be necessary, and one can now use tsconfig files instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants