Skip to content
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

Trying to augment a module causes @parcel/transformer-typescript-types to output a mostly blank .d.ts file #7306

Closed
astegmaier opened this issue Nov 15, 2021 · 0 comments

Comments

@astegmaier
Copy link
Contributor

astegmaier commented Nov 15, 2021

🐛 bug report

If you try to use the module augmentation feature of typescript in the context of a library project that is using @parcel/transformer-typescript-types to generate a .d.ts file, parcel will output an incorrect .d.ts file with many elements missing.

🎛 Configuration (.babelrc, package.json, cli command)

Create a library-original package with the following export:

export class Person {
    constructor(public name: string) {}
}

Build a library-augmenter package with parcel:

package.json

{
  "name": "library-augmenter",
  "types": "dist/index.d.ts",
  "source": "src/index.ts",
  "main": "dist/index.js",
  "scripts": {
    "build": "parcel build",
  },
  "dependencies": {
    "library-original": "1.0.0"
  },
  "devDependencies": {
    "@parcel/packager-ts": "^2.0.1",
    "@parcel/transformer-typescript-types": "^2.0.1",
    "parcel": "^2.0.1",
  }
}

src/index.ts

import { Person } from "library-original";
Person.prototype.greet = function() { return `Hello ${this.name}!` }

// This will be present in the .d.ts file generated by parcel:
export const anotherThing: string = "hello";

// This will get dropped:
declare module "library-original" {
  interface Person {
    greet(): string;
  }
}

// This will also get dropped, if the "declare module" statement above is present:
export const somethingElse: string = "goodbye";

🤔 Expected Behavior

Parcel should ouput a complete .d.ts file, similar to tsc:

export declare const anotherThing: string;
declare module "library-original" {
    interface Person {
        greet(): string;
    }
}
export declare const somethingElse: string;

😯 Current Behavior

Parcel outputs a .d.ts file that is missing both the module augmentation code and any other exports that are declared after it:

export const anotherThing: string;

💁 Possible Solution

It looks like we're doing some tree shaking of the .d.ts file in these lines, and we probably didn't account for this case.

let emitResult = program.emit(undefined, undefined, undefined, true, {
afterDeclarations: [
// 1. Build module graph
context => sourceFile => {
return collect(moduleGraph, context, sourceFile);
},
// 2. Tree shake and rename types
context => sourceFile => {
return shake(moduleGraph, context, sourceFile);
},
],
});

🔦 Context

I was trying to help answer this question on stackoverflow

💻 Code Sample

See this repo for a reproduction of the issue.

🌍 Your Environment

Software Version(s)
Parcel 2.0.1
Node 14.17.5
Yarn 1.22.15
Operating System MacOS Monterey 12.0.1
@astegmaier astegmaier changed the title Trying to augment a module causes @parcel/transformer-typescript-types to output mostly blank .d.ts file Trying to augment a module causes @parcel/transformer-typescript-types to output a mostly blank .d.ts file Nov 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants