Skip to content

Emitting combined output declarations tries to resolve monorepo imports #39547

Closed

Description

TypeScript Version: 4.0.0-dev.20200710

Search Terms: emit declaration, combined output, monorepo, outFile

Code

tsc index.ts --declaration --emitDeclarationOnly --outFile dist/index.d.ts

// packages/a/src/index.ts
import { B } from "b"; // this imports another package in the monorepo

export default function () {
	return new B();
}
// packages/b/package.json
{
	"name": "b",
	"version": "0.0.0",
	"source": "src/index.ts",
	"types": "dist/index.d.ts", // doesn't exist yet
	"main": "dist/index.js"     // doesn't exist yet
}

The files listed in the package.json of b don't exist yet because when building all packages in this monorepo setup, a is built before b.

Full repro in https://github.com/mischnic/parcel-issue-3891

Strangely enough, when omitting the --outFile flag, the declaration file for packages/a/src/index.ts (correctly) contains import ... from "b"; (this suggests that this is indeed a bug and not per design).

Expected behavior:

a/dist/index.d.ts output:

declare module "index" {
    import { B } from "b";
    export default function (): B;
}

Actual behavior:

a/dist/index.d.ts output:

declare module "index" {
    import { B } from "../../b/index"; // <---
    export default function (): B;
}

Playground Link:
https://github.com/mischnic/parcel-issue-3891

Related Issues: Not really related: #34846

Activity

  1. mischnic commented on Jul 10, 2020

    @mischnic
    Author

    (I'd be happy to fix this myself if someone can confirm this and point me to the right source files)

  2. RyanCavanaugh commented on Jul 15, 2020

    @RyanCavanaugh
    Member

    Using outFile with modules is almost always a mistake; what are you intending to do with that setting?

  3. mischnic commented on Jul 15, 2020

    @mischnic
    Author

    Thanks for the response!

    We're using it in Parcel to essentially create a definitions bundle (as opposed to multiple files) for a package, e.g.

    // index.ts
    import { B } from "b";
    export { thing } from "./other";
    
    export default function () {
    	return new B();
    }
    // other.ts
    export function thing(){
    	return "abc";
    }

    --tsc-->

    declare module "other" {
        export function thing(): string;
    }
    declare module "index" {
        import { B } from "b";
        export { thing } from "other";
        export default function (): B;
    }

    --ast transformation-->

    import { B } from "b";
    export function thing(): string;
    export default function (): B;

    (https://github.com/parcel-bundler/parcel/blob/1f5586657f1ea8660ca1f06abdd75687851ebbb2/packages/transformers/typescript-types/src/TSTypesTransformer.js#L28-L69)

    EDIT:

    I just noticed that import .. from "./b" and import ... from "b" are (independent from this issue) both transpiled to "b" in the declarations output....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Fix AvailableA PR has been opened for this issueNeeds InvestigationThis issue needs a team member to investigate its status.RescheduledThis issue was previously scheduled to an earlier milestone

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions