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

Support making type parameters private #1319

Closed
d-fischer opened this issue Jun 10, 2020 · 1 comment
Closed

Support making type parameters private #1319

d-fischer opened this issue Jun 10, 2020 · 1 comment
Labels
wontfix Declining to implement

Comments

@d-fischer
Copy link

Problem

Suppose I have a class like this (this is from my actual generated .d.ts but a bit simplified):

export default abstract class Subscription</** @protected */ T = any> {
    protected _handler: (obj: T) => void;
    /** @protected */
    constructor(_handler: (obj: T) => void);
    /**
     * Whether the subscription has been verified.
     */
    get verified(): boolean;
    /**
     * Activates the subscription.
     */
    start(): Promise<void>;
    /**
     * Suspends the subscription, not removing it from the listener.
     */
    suspend(): Promise<void>;
    /**
     * Deactivates the subscription and removes it from the listener.
     */
    stop(): Promise<void>;

    protected abstract transformData(response: object): T;
}

The Subscription class uses the T type parameter internally, but not at all in its public API.

For consumers reading the documentation, it might be unnecessarily confusing to include it.

Suggested Solution

Expose the @protected (or @private) tag in the output somehow.

(This is coming from the Discord conversation we had a few days ago, just wanna formalize the suggestion here and provide an example)

@d-fischer d-fischer added the enhancement Improved functionality label Jun 10, 2020
@Gerrit0 Gerrit0 added the wontfix Declining to implement label Feb 15, 2021
@Gerrit0
Copy link
Collaborator

Gerrit0 commented Feb 15, 2021

Have thought about this off and on for a while, and eventually determined that I'm not going to include this in TypeDoc proper. I want to focus on properly documenting what TypeScript sees as the export, which means limiting the scope of features that aim to modify the documentation away from that.

That said, this isn't too difficult to write a plugin that will do this for you:

typedoc --plugin ./ignore-type-params.js src/index.ts
const { Converter, ReflectionKind } = require("typedoc")

exports.load = function({ application }) {
        const ignoredTypeParameters = []

        application.converter.on(Converter.EVENT_CREATE_TYPE_PARAMETER, (_ctx, param, node) => {
                if (node?.getFullText().includes("@ignore")) {
                        ignoredTypeParameters.push(param)
                }
        })

        application.converter.on(Converter.EVENT_RESOLVE_BEGIN, () => {
                for (const param of ignoredTypeParameters) {
                        removeIfPresent(param.parent.typeParameters, param)
                        const ctor = param.parent.children?.find(r => r.kindOf(ReflectionKind.Constructor))
                        for (const s of ctor.signatures ?? []) {
                                removeIf(s.typeParameters, p => p.name === param.name)
                        }
                }
                ignoredTypeParameters.length = 0
        })
}

function removeIf(arr, fn) {
        const index = arr?.findIndex(fn) ?? -1
        if (index !== -1) {
                arr.splice(index, 1)
        }
}

function removeIfPresent(arr, item) {
        const index = arr?.indexOf(item) ?? -1
        if (index !== -1) {
                arr.splice(index, 1)
        }
}

@Gerrit0 Gerrit0 closed this as completed Feb 15, 2021
@Gerrit0 Gerrit0 removed the enhancement Improved functionality label Feb 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix Declining to implement
Projects
None yet
Development

No branches or pull requests

2 participants