Skip to content

Tooltip content is typed string but renders any ReactNode #230

Description

@einari

What happens

Tooltip's content is typed string, but the implementation renders it straight through as the
children of Tooltip.Popup:

export interface TooltipProps {
    content?: string;
    ...
}
jsx(Tooltip$1.Popup, { className: "cratis-tooltip-popup", children: content })

So any ReactNode renders correctly at runtime - the type is simply narrower than the component.

Why it matters

Help content is the natural case: a tooltip holding a couple of paragraphs and a "Learn more" link
is ordinary, and it works today - but only if you cast:

<Tooltip content={help as unknown as string} position='right'>

That cast is not describing anything true, and it silences the compiler for the whole expression,
so a genuine mistake in help would no longer be caught. A consumer that does not want the cast has
to either flatten rich help into a string or stop using the wrapper, which is the thing the wrapper
exists to prevent (primereact/tooltip directly).

Suggested

Widen to content?: React.ReactNode. It matches what the component already does, is source-compatible
for every existing call site (string is a ReactNode), and needs no runtime change.

Observed on @cratis/components as resolved in an application today; worked around locally with the
cast above, referencing this issue.

Activity

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

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions