Skip to content

When using packages allow allow linking to types between different packages #1612

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
patrickarlt opened this issue Jun 24, 2021 · 7 comments
Closed

Comments

@patrickarlt
Copy link

Search Terms

package, link

Problem

I have a large monorepo with several packages that include a shared types package to handle common interfaces that are used multiple packages. I have a super minimal reproduction of the issue in https://github.com/patrickarlt/typedoc-cross-link-packages-demo/ and hosted the TypeDoc at https://patrickarlt.github.io/typedoc-cross-link-packages-demo/index.html

Essentially the following is happening:

// export shared types from packages/shared/index.ts
export interface IceCream {
  flavor: Flavors;
}

export enum Flavors {
  chocolate,
  vanilla,
}
// import shared types and use them in packages/factory/index.ts
import { IceCream, Flavors } from "../shared";

// ideally `Flavors` and `IceCream` would link to the doc in the shared package but they do not.
export function make(flavor: Flavors): IceCream {
  return {
    flavor: flavor,
  };
}

2021-06-24_11-12-48

However if I re-export the shared types they get documented but as a part of the package they are exported from not the shared package.

// import shared types and use them in packages/store/index.ts
import { IceCream, Flavors } from "../shared";

// re-export so they get documented as a part of the store package
export { IceCream, Flavors } from "../shared";

export function serve(flavor: Flavors): IceCream {
  return {
    flavor: flavor,
  };
}

2021-06-24_11-16-10

Suggested Solution

If a file in a package imports from another package that is in the packages array then any doc using that type should link to that package in the generated documentation.

I would also be totally fine if there was a plugin that provided this behavior but I couldn't fine one and it seems like this should be core behavior since I am documenting the shared package with TypeDoc.

If this should go in a plugin I would be happy for some pointers beyond the existing plugin doc to get started developing a plugin that does this.

@Gerrit0
Copy link
Collaborator

Gerrit0 commented Jun 25, 2021

This is definitely something that should live in TypeDoc itself - unfortunately it's kind of tricky to do... right now we resolve references based on symbols, and TS uses different symbols for each program (package)

@patrickarlt
Copy link
Author

@Gerrit0 I saw that when I was messing around with this a bit more yesterday. Is this something you would need fixed in the TypeScript compiler before it could be fixed in TypeDoc? I don't mind exporting the extra interfaces but at scale someone will forget to export and it won't get linked. FWIW api-extractor doesn't do this either so it might just be too difficult.

The workaround to simply re-export works for now but it would be great if this could be resolved in the future.

@Gerrit0 Gerrit0 changed the title When using packages allow allow linking to types between differnet packages When using packages allow allow linking to types between different packages Jun 26, 2021
@Gerrit0
Copy link
Collaborator

Gerrit0 commented Jun 26, 2021

No, the compiler is behaving exactly as it should. The programs are entirely separate so they really should be separate. This resolution is something TypeDoc would have to do, likely by making some assumptions about things with the same name/position in a file with the same basename being the same symbol

Something that might possibly work well is setting up a separate project with project references to each of your packages and having an entry point which re-exports each package, which if TS resolves references properly, might let the types be linked

@efokschaner
Copy link
Contributor

I ran in to something possibly similar when we implemented "packages" support. See "Inter-package references" in #1567 (comment)

I wondered if declaration maps could solve this (https://www.typescriptlang.org/tsconfig#declarationMap)
I think that's how similar tooling such as vscode makes the Inter-package references work smoothly.

@Gerrit0
Copy link
Collaborator

Gerrit0 commented Jul 31, 2021

declaration maps are definitely going to be part of the answer, but they aren't sufficient without some additional work to stop using symbols.

@haltman-at
Copy link

We're having a similar problem. I forget the details of our workaround for this, but it's fairly hacky and involves relying on the discouraged type resolution step 4. Just chiming in to say it would be nice to have a proper solution for this.

@Gerrit0
Copy link
Collaborator

Gerrit0 commented Oct 23, 2022

Going to close this one in favor of #1835

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

4 participants