How to extend JSX.IntrinsicElements['div']? TS2499
Asked Answered
C

2

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?

Calif answered 13/5, 2019 at 2:10 Comment(1)
extends React.ComponentPropsWithoutRef<'div'> works too IIRCCalif
F
11

What worked for me was defining the type before hand

type SpanProps = JSX.IntrinsicElements['span']

interface IconProps extends SpanProps {
  weight?: string
  src: string
}
Fluecure answered 18/8, 2019 at 5:28 Comment(0)
B
-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]; };
Blowsy answered 25/8, 2021 at 9:31 Comment(1)
Is that not just a re-export of the JSX.IntrinsicElements that comes with React?Calif

© 2022 - 2024 — McMap. All rights reserved.