51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

如何允许特定组件类型通过 TypeScript 检查?

英文:

react-typescript - How do I allow a specific component type to pass typescript check?

问题 {#heading}

我相对于TypeScript还比较新,尽管在React(和prop-types)方面有很长时间的经验。

我遇到了一个关于为我的组件定义类型的问题,如果有另一个组件作为属性。我已经有一个已定义typeButton组件。

现在的问题是,当我创建一个新的组件ButtonGroup,它具有一个children属性,该属性只允许渲染Button组件。我正在将子组件的属性与Button类型的属性进行比较 - 这意味着我可以传递另一个组件,如Input,如果我不破坏按钮属性 - 就像<Input id="input-id" />对TypeScript来说是可以的,但我不想允许这样做,因为它是一种不同类型的组件。

我已经成功地使用了component.type.displayName,但TypeScript完全不喜欢这个解决方案。

import { ReactElement, Children } from 'react'

type buttonVariantType = 'primary' | 'secondary' | undefined
type buttonProps = {
  id?: string
  variant?: buttonVariantType 
  children: ReactNode
  // ...
}

const Button = ({ children, id, variant = 'secondary' }) => {
  // ...
  return <button ...>{children}</button>
}

interface buttonGroupProps {
  children: ReactElement<buttonProps>[]
  variant?: buttonVariantType 
}

const ButtonGroup = ({ children, variant }: buttonGroupProps) => {
  return (
    <div>
      {Children.map(children, (child) => {
        // TS2339: Property 'displayName' does not exist on type 'string | JSXElementConstructor<any>'
        if (child?.type.displayName !== 'Button') {
          console.log(child.type.displayName, 'not a button')
          throw Error('not a button') // 丑陋的解决方法,TypeScript仍然不接受
        }
        return (
          <Button {...child?.props} variant={variant ?? child.props.variant}>
            {child.props.children}
          </Button>
        )
      })}
    </div>
  )
}

现在,如果我将另一个组件作为ButtonGroupchildren放入,TypeScript会接受,我不希望发生这种情况。

<ButtonGroup variant="danger">
  <Input id="123" /> 
  <Button variant="primary" id="primary">
    Primary button
  </Button>
  <Button variant="secondary" id="secondary">
    Secondary button
  </Button>
</ButtonGroup>

英文:

I'm relatively new to typescript, although having a long experience with react (and prop-types).

I've encountered a problem with typing my component, if there's another component as a prop. I already have a Button component with a defined type.

Now the problem is, when I create a new component ButtonGroup having a children prop that only should allow Button components to be rendered. I'm comparing the child's props to the Button type props - this means that I can pass there another component like Input if I don't break a button prop - like having &lt;Input id=&quot;input-id&quot; /&gt; is fine for typescript, but I don't want to allow that, because it's a different type of component.

I've successfully played with the component.type.displayName, but Typescript doesn't like this solution at all.

import { ReactElement, Children } from &#39;react&#39;

type buttonVariantType = \&#39;primary\&#39; \| \&#39;secondary\&#39; \| undefined
type buttonProps = {
id?: string
variant?: buttonVariantType
children: ReactNode
...
}


const Button =({ children, id, variant = \&#39;secondary\&#39; }) =\&gt; {
...
return \&lt;button ...\&gt;{children}\&lt;/button\&gt;
}


interface buttonGroupProps {
children: ReactElement\&lt;buttonProps\&gt;\[\]
variant?: buttonVariantType
}

`const ButtonGroup = ({ children, variant }: buttonGroupProps) =&gt; {
return (
&lt;div&gt;
{Children.map(children, (child) =&gt; {
// TS2339: Property &#39;displayName&#39; does not exist on type &#39;string | JSXElementConstructor&lt;any&gt;&#39;
if (child?.type.displayName !== &#39;Button&#39;) {
console.log(child.type.displayName, &#39;not a button&#39;)
throw Error(&#39;not a button&#39;) // ugly workaround, still unacceptable by typescript
}
return (
&lt;Button {...child?.props} variant={variant ?? child.props.variant}&gt;
{child.props.children}
&lt;/Button&gt;
)
})}
&lt;/div&gt;
)
}
`

Now if I put another component as a children into ButtonGroup, the typescript is okay with that and I don't want that.

&lt;ButtonGroup variant=&quot;danger&quot;&gt;
  &lt;Input id=&quot;123&quot; /&gt; 
  &lt;Button variant=&quot;primary&quot; id=&quot;primary&quot;&gt;
    Primary button
  &lt;/Button&gt;
  &lt;Button variant=&quot;secondary&quot; id=&quot;secondary&quot;&gt;
    Secondary button
  &lt;/Button&gt;
&lt;/ButtonGroup&gt;

答案1 {#1}

得分: 0

以下是您要翻译的代码部分:

import * as React from "react";

type buttonProps = {
id?: string;
variant?: 'primary' \| 'secondary';
children?: React.ReactNode;
}


const Button = ({ children, id, variant = 'secondary' }: buttonProps) =\> {
return \<button\>{children}\</button\>
}


const div = () =\> \<div\>\</div\>;
const input = () =\> \<input\>\</input\>;
const Btn = () =\> \<Button\>\</Button\>;


type Button = React.ReactElement\<buttonProps, React.JSXElementConstructor\<buttonProps\>\>;
type Div = ReturnType\<typeof div\>;
type Input = ReturnType\<typeof input\>;
type Btn = ReturnType\<typeof Btn\>;


type ExpandedButton = { \[Property in keyof Button\]: Button\[Property\] }


type ExpandedDiv = { \[Property in keyof Div\]: Div\[Property\] };


type ExpanedInput = { \[Property in keyof Input\]: Input\[Property\] };


type ExpandedBtn = { \[Property in keyof Btn\]: Btn\[Property\] };

`type Assignable = (ExpandedButton extends (ExpandedDiv | ExpanedInput | ExpandedBtn | Div | Input | Btn) ? true : false) | ((ExpandedDiv | ExpanedInput | ExpandedBtn | Div | Input | Btn) extends ExpandedButton ? true : false);
`

import { ReactElement, Children } from 'react'

type buttonVariantType = 'primary' | 'secondary' | undefined
type buttonProps = {
  id?: string
  variant?: buttonVariantType 
  children?: React.ReactNode
}

const Button =({ children, id, variant = 'secondary' }: buttonProps) => {
  return <button>{children}</button>
}

interface buttonGroupProps {
  children: ReactElement<buttonProps>[]
  variant?: buttonVariantType 
}

const ButtonGroup = ({ children, variant }: buttonGroupProps) => {
  return (
    <div>
      {Children.map(children, (child) => {

        if (child?.type !== Button ) {
          throw Error('not a button')
        }

        return (
          <Button {...child?.props} variant={variant ?? child.props.variant}>
            {child.props.children}
          </Button>
        )
      })}
    </div>
  )
}

英文:

It's not possible to make it compile time safe,reason for that is, when you declare any component with the jsx tags, the type that's created through that contains any for the values in the type. Let me give you an example -

import * as React from &quot;react&quot;;

type buttonProps = {
id?: string;
variant?: \&#39;primary\&#39; \| \&#39;secondary\&#39;;
children?: React.ReactNode;
}


const Button = ({ children, id, variant = \&#39;secondary\&#39; }: buttonProps) =\&gt; {
return \&lt;button\&gt;{children}\&lt;/button\&gt;
}


const div = () =\&gt; \&lt;div\&gt;\&lt;/div\&gt;;
const input = () =\&gt; \&lt;input\&gt;\&lt;/input\&gt;;
const Btn = () =\&gt; \&lt;Button\&gt;\&lt;/Button\&gt;;


type Button = React.ReactElement\&lt;buttonProps, React.JSXElementConstructor\&lt;buttonProps\&gt;\&gt;;
type Div = ReturnType\&lt;typeof div\&gt;;
type Input = ReturnType\&lt;typeof input\&gt;;
type Btn = ReturnType\&lt;typeof Btn\&gt;;


type ExpandedButton = { \[Property in keyof Button\]: Button\[Property\] }
//   \^? type ExpandedButton = { type: React.JSXElementConstructor\&lt;buttonProps\&gt;; props: buttonProps; key: React.Key \| null; }


type ExpandedDiv = { \[Property in keyof Div\]: Div\[Property\] };
//   \^? type ExpandedDiv = { type: any; props: any; key: React.Key \| null; }


type ExpanedInput = { \[Property in keyof Input\]: Input\[Property\] };
//   \^? type ExpanedInput = { type: any; props: any; key: React.Key \| null;


type ExpandedBtn = { \[Property in keyof Btn\]: Btn\[Property\] };
//   \^? type ExpanedBtn = { type: any; props: any; key: React.Key \| null;

`type Assignable = (ExpandedButton extends (ExpandedDiv | ExpanedInput | ExpandedBtn | Div | Input | Btn) ? true : false) | ((ExpandedDiv | ExpanedInput | ExpandedBtn | Div | Input | Btn) extends ExpandedButton ? true : false);
//   ^? type Assignable = true
`

Which goes to show, any jsx element can be assigned to the other. As for a runtime error, you got it nearly right, the check that you should be making is to see if the component that's the child is actually the Button component through reference equality check, like this -

import { ReactElement, Children } from &#39;react&#39;

type buttonVariantType = \&#39;primary\&#39; \| \&#39;secondary\&#39; \| undefined
type buttonProps = {
id?: string
variant?: buttonVariantType
children?: React.ReactNode
}


const Button =({ children, id, variant = \&#39;secondary\&#39; }: buttonProps) =\&gt; {
return \&lt;button\&gt;{children}\&lt;/button\&gt;
}


interface buttonGroupProps {
children: ReactElement\&lt;buttonProps\&gt;\[\]
variant?: buttonVariantType
}


const ButtonGroup = ({ children, variant }: buttonGroupProps) =\&gt; {
return (
\&lt;div\&gt;
{Children.map(children, (child) =\&gt; {


        if (child?.type !== Button ) {
          throw Error(&amp;#39;not a button&amp;#39;)
        }

        return (
          &amp;lt;Button {...child?.props} variant={variant ?? child.props.variant}&amp;gt;
            {child.props.children}
          &amp;lt;/Button&amp;gt;
        )
      })}
    &amp;lt;/div&amp;gt;



`)
}
`


赞(0)
未经允许不得转载:工具盒子 » 如何允许特定组件类型通过 TypeScript 检查?