9

How come I can create a new type via JSX.IntrinsicElements['div'] & X but I can't extend it?

I don't understand the error message -- am I not simply adding an "optional type"? What's the difference between extending types these 2 different ways?

1
  • 1
    extends React.ComponentPropsWithoutRef<'div'> works too IIRC
    – mpen
    Commented Jul 9, 2021 at 21:29

2 Answers 2

11

What worked for me was defining the type before hand

type SpanProps = JSX.IntrinsicElements['span']

interface IconProps extends SpanProps {
  weight?: string
  src: string
}
-1

You can use React-Markdown:

import { IntrinsicElements } from "react-markdown/src/ast-to-react";

interface InputContainerProps1 extends Partial<IntrinsicElements['div']>{
  minWidth?: string | number
}

The Partial Class constructs a type with all properties of IntrinsicElements['div'] set to optional. Under the hood the Partial interface looks like this:

type Partial<T> = { [P in keyof T]?: T[P]; };

1
  • Is that not just a re-export of the JSX.IntrinsicElements that comes with React?
    – mpen
    Commented Aug 25, 2021 at 22:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.