简体   繁体   English

根据 props 显示不同的组件:TypeError

[英]Display different components depending on props: TypeError

I am currently trying to create a type safe implementation of an component that takes its props and displays different components depending on its props.我目前正在尝试创建一个组件的类型安全实现,该组件采用其道具并根据其道具显示不同的组件。

I have tried a number of different approaches but no matter what, I get TypeErrors.我尝试了多种不同的方法,但无论如何,我都会遇到 TypeErrors。

My current approach is this:我目前的做法是:

import React from 'react';

interface Props<brand, type> {
    identification: {
        brand: brand,
        type: type
    };
    id: number;
}

type Comp1Props = Props<'one', 'a'>;

const Comp1:React.FC<Comp1Props> = props => <div>{JSON.stringify(props)}</div>;

type Comp2Props = Props<'one', 'b'>;

const Comp2:React.FC<Comp2Props> = props => <div>{JSON.stringify(props)}</div>;

type Comp3Props = Props<'two', 'b'>;

const Comp3:React.FC<Comp3Props> = props => <div>{JSON.stringify(props)}</div>;

type AllComps = Comp1Props | Comp2Props | Comp3Props;

const Panel:React.FC<AllComps> = (props) => {
    const { brand, type } = props.identification;
    if( brand === 'one' ){
        if (type === 'a') {
            return <Comp1 {...props} />; // TypeError
        }
        if (type === 'b') {
            return <Comp2 {...props} />; // TypeError
        }
    }
    if( brand === 'two' ){
        if (type === 'b') {
            return <Comp3 {...props} />; // TypeError
        }
    }
    return <div />;
};

const Display: React.FC<AllComps> = (props) => (
    <Panel {...props} />
)

export default Display;

I get following Error Message:我收到以下错误消息:

Type '{ identification: { brand: "one"; type: "a"; }; id: number; } | { identification: { brand: "one"; type: "b"; }; id: number; } | { identification: { brand: "two"; type: "b"; }; id: number; }' is not assignable to type 'IntrinsicAttributes & Comp1Props'.
  Type '{ identification: { brand: "one"; type: "b"; }; id: number; }' is not assignable to type 'Comp1Props'.
    The types of 'identification.type' are incompatible between these types.
      Type '"b"' is not assignable to type '"a"'.ts(2322)

I am bumping my heads against a wall for hours now, not finding a sufficient solution to make a concept like this possible and type safe, I guess I am having some fundamental misunderstanding here.几个小时以来,我一直在用头撞墙,没有找到足够的解决方案来使这样的概念成为可能并且类型安全,我想我在这里有一些根本性的误解。

I would really appreciate all information that possibly leads me to a solution for this kind of Problem.我真的很感激所有可能导致我解决此类问题的信息。 I can't imagine I am the first one who tries to create a 'switch-component' with more than one switch-prop.我无法想象我是第一个尝试使用多个 switch-prop 创建“switch-component”的人。

Thanks a lot!非常感谢!

Destructuring messes up with typescript解构弄乱了 typescript

Try:尝试:

const Panel:React.FC<AllComps> = (props) => {
    if( props.identification.brand === 'one' ){
        if (props.identificationtype === 'a') {
            return <Comp1 {...props} />;
        }
        if (props.identification.type === 'b') {
            return <Comp2 {...props} />;
        }
    }
    if( props.identification.brand === 'two' ){
        if (props.identification.type === 'b') {
            return <Comp3 {...props} />;
        }
    }
    return <div />;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM