Skip to content

Improve ergonomics when using exactOptionalPropertyTypes #1916

Open
@OliverJAsh

Description

@OliverJAsh

Bug report

Current behavior

In projects where exactOptionalPropertyTypes is enabled, we frequently encounter errors like this:

import { Menu } from '@base-ui-components/react';
import type { FC, ReactNode } from 'react';
import * as styles from './MenuItem.module.css';

export const MenuItem: FC<{ disabled?: boolean; children: ReactNode }> = ({
  disabled,
  children,
}) => (
  // Type 'undefined' is not assignable to type 'boolean'.
  <Menu.Item disabled={disabled} className={styles.item}>
    {children}
  </Menu.Item>
);
import { Tooltip } from '@base-ui-components/react';
import type { FC, ReactNode } from 'react';

export const MyTooltip: FC<{
  anchor?: HTMLElement;
  children: ReactNode;
}> = ({ anchor, children }) => (
  <Tooltip.Portal>
    {/* Type 'undefined' is not assignable to type 'Element | VirtualElement | RefObject<Element | null> | (() => Element | VirtualElement | null) | null'. */}
    <Tooltip.Positioner anchor={anchor}>
      <Tooltip.Popup>{children}</Tooltip.Popup>
    </Tooltip.Positioner>
  </Tooltip.Portal>
);

It's possible to workaround these errors using spread syntax, but it's very ugly, and we forgo excess property checking.

Expected behavior

The examples above should not error.

Optional properties should also allow explicit undefined, as is the case for props like className on built-in elements such as div.

Reproducible example

https://codesandbox.io/p/sandbox/condescending-glade-ryyx5d

Base UI version

1.0.0-alpha.8

Which browser are you using?

N/A

Which OS are you using?

N/A

Which assistive tech are you using (if applicable)?

N/A

Additional context

N/A

Metadata

Metadata

Assignees

No one assigned

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions