Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

129
Visualizações
TypeScript: The object properties in the array are interdependent

The object properties in the array are interdependent eg when call AComponent

I want componentProps must be Type1Props, when items.type === 'type1', if items.type === 'type2' componentProps shoud be Type2Props

//I want componentProps must be Type1Props, when ` items.type === 'type1' `, if `items.type === 'type2'` componentProps shoud be Type2Props
<AComponent items= {[
{type:'type1', componentProps{  }}
{type:'type2', componentProps{  }
]}>

Below is AComponent.tsx code

// if type === type1 the value must be constraint with Type1Props
type ComTypes = {
   type1 : Type1Props,
   type2 : Type1Props,
   type2 : Type1Props,
}

type ItemType =
    'type1'
    | 'type2'
    | 'type3'
  

//  The object properties in the array
type ItemProps<T> = {
    type: T
    componentProps: ComTypes[T]
    showItem?: boolean
}

interface Props extends FormProps {
    itemsArray: QDItemProps[]
}

const AComponent: FC<Props> = ({itemsArray}) => {

}
export default AComponent

I do not know how to solove this scene

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use union types to do this. I'll give you a more generic example that is not using react, for the benefit of others who may stumble upon this question.


type Type1Props = "A";
type Type2Props = "B";
type Type3Props = "C";

type PropTypes =
  | {
      type: "type1",
      componentProps: Type1Props,
    }
  | {
      type: "type2",
      componentProps: Type2Props,
    }
  | {
      type: "type3",
      componentProps: Type3Props,
    };

const p1: PropTypes = {
  type: "type1",
  componentProps: "A",
};

const p2: PropTypes = {
  type: "type2",
  componentProps: "B",
};

You should be able to extend this for your use case.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda