Skip to content

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

Closed
@sno2

Description

@sno2

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions