Skip to content

fix: Support elementSummaries for inlined union members #2933

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
wants to merge 1 commit into from

Conversation

LekoArts
Copy link

Hello!

I've noticed a bug for a use case like this:

/**
 * @inline
 */
type TestReturn =
    /**
     * An apple a day keeps the doctor away.
     */
    | { kind: 'apple', isHealthy: true }
    /**
     * A donut a day keeps the doctor not away.
     */
    | { kind: 'sweets', isHealthy: false }

/**
 * Returns a random value
 */
export function test(): TestReturn {
    return Math.random() > 0.5
        ? { kind: 'apple', isHealthy: true }
        : { kind: 'sweets', isHealthy: false };
}

The type.types inside the signature doesn't receive the elementSummaries for the union type. I'm adding a failing test in this PR first to showcase the problem and show that it fails.

I'll spend some time to see if I can find the fix in this (so far unfamiliar) codebase. If not, I'll let you know 👍

@LekoArts
Copy link
Author

LekoArts commented Apr 11, 2025

Mh, I do wonder, is the current behavior intentional because it's not possible?

Reading

/**
* If present, there should be as many items in this array as there are items in the {@link types} array.
*
* This member is only valid on unions which are on {@link DeclarationReflection.type | DeclarationReflection.type} with a
* {@link ReflectionKind} `kind` of `TypeAlias`. Specifying it on any other union is undefined behavior.
*/
elementSummaries?: CommentDisplayPart[][];
I'm wondering if what I want can even be achieved.

I can workaround this limitation by using a custom blockTag:

/**
 * Returns a random value
 *
 * @unionReturnHeadings
 * ["An apple a day keeps the doctor away.", "A donut a day keeps the doctor not away."]
 */
export function test(): TestReturn {
    return Math.random() > 0.5
        ? { kind: 'apple', isHealthy: true }
        : { kind: 'sweets', isHealthy: false };
}

@Gerrit0
Copy link
Collaborator

Gerrit0 commented Apr 11, 2025

Yeah, this is intentional. Supporting comments on union elements only makes sense if TypeDoc is converting based on the type node rather than the actual type. This only happens in a few select cases, and only actually needs to happen for type aliases, so it is only valid to include it there.

TypeDoc doesn't support rendering the union comments anywhere except type alias types and I didn't want people expecting it to work in other locations, which is why that comment says what it does.

@LekoArts
Copy link
Author

Ok, thanks for enlightening me. Then I'll close it :)

@LekoArts LekoArts closed this Apr 12, 2025
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

Successfully merging this pull request may close these issues.

2 participants