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

'required' attribute set to false when intersection used #339

Closed
leepowelldev opened this issue Apr 2, 2021 · 6 comments
Closed

'required' attribute set to false when intersection used #339

leepowelldev opened this issue Apr 2, 2021 · 6 comments
Labels
stale There is no activity for a long time.

Comments

@leepowelldev
Copy link

Sorry, this is a bit vague ... but I have the following component:

import { HTMLAttributes, ReactNode } from 'react';

type Props = {
  children: ReactNode;
  title: string;
};

const Test = ({ children, title, ...rest }: Props) => (
  <div {...rest}>
    <h2>{title}</h2>
    {children}
  </div>
);

Test.displayName = 'Test';

export { Test };

In this instance, both children and title are correctly marked as required. However, when I use an intersection type like so to allow additional, valid html props ...

import { HTMLAttributes, ReactNode } from 'react';

type Props = {
  children: ReactNode;
  title: string;
} & HTMLAttributes<HTMLDivElement>;

const Test = ({ children, title, ...rest }: Props) => (
  <div {...rest}>
    <h2>{title}</h2>
    {children}
  </div>
);

Test.displayName = 'Test';

export { Test };

... then for some reason both children and title are no longer marked as required. Am I doing something wrong to cause this?

@leepowelldev
Copy link
Author

I have found a workaround:

interface Props extends HTMLAttributes<HTMLDivElement> {
  children: ReactNode;
  title: string;
}

@leepowelldev
Copy link
Author

Another oddity, when children is set to something other than ReactNode and a intersection is used I seem to get an intersection type returned:

type Props = {
  children: string;
  title: string;
} & HTMLAttributes<HTMLDivElement>

In this instance I would get children as type string & ReactNode

Screenshot 2021-04-02 at 12 15 56

However, using extends works as expected:

Screenshot 2021-04-02 at 12 17 56

@github-actions
Copy link

github-actions bot commented Oct 9, 2021

There was no activity for a long time. Closing this issue as obsolete. In case it is still valid, please, open a new one.

@github-actions github-actions bot added the stale There is no activity for a long time. label Oct 9, 2021
@pvasek pvasek removed the stale There is no activity for a long time. label Oct 9, 2021
@Kosai106
Copy link

I could be wrong but I believe the reason for this is that HTMLAttributes already provides an optional title prop, which is overwriting yours. HTMLAttributes is also extending DOMAttributes internally which provides an optional children prop that is again, overwriting yours.

The fix would be in the order of your props:

type Props = HTMLAttributes<HTMLDivElement> & {
  children: ReactNode;
  title: string;
};

@github-actions
Copy link

There was no activity for a long time. The issue will be closed soon.

@github-actions github-actions bot added the stale There is no activity for a long time. label Mar 30, 2023
@github-actions
Copy link

github-actions bot commented Apr 6, 2023

Closing this issue as obsolete.

@github-actions github-actions bot closed this as completed Apr 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale There is no activity for a long time.
Projects
None yet
Development

No branches or pull requests

3 participants