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

113
Visualizações
Typescript issue with using multiple types

Been racking my brain for hours on this, basically, I have a bunch of different types defined like this:

export type SharedAPIProps = {
  id: string;
};

export type ButtonAPIProps = SharedAPIProps & {
  type: "paragraph--button";
  field_title: string;
};

export type SlideAPIProps = SharedAPIProps & {
  type: "paragraph--slide";
  field_slides: []
};

export type allAvailableComponents = ButtonAPIProps | SlideAPIProps;


export type ButtonProps = {
  title: string;
}

export type SlideProps = {
  slides: [];
} 

export type allCleanedProps = ButtonProps | SlideProps;

Then I have a function to convert an API response to a clean version:

export const toCleanProps = (component: allAvailableComponents): allCleanedProps => {
  const { type } = component;
  
  const convertedProps: allCleanedProps = {} as allCleanedProps;

  switch (type) {
    case "paragraph--button":
      convertedProps.title = component.field_title;
      break;
    case "paragraph--slide":
      convertedProps.slides = component.field_slides;
      break;
  }

  return convertedProps;
}

The issue is I'm getting Typescript errors on every property of the convertedProps. When I have just a single type defined in allComponentProps like:

export type allAvailableComponents = ButtonAPIProps;

I don't get the errors on the paragraph--button convertedProps properties, but of course do on the paragraph--slide convertedProps properties.

New to Typescript, so bear with me, I'm probably missing something simple here and tried a bunch of different things, but none work.

Error message being those props don't exist on the defined types.

Here's the complete example: https://www.typescriptlang.org/play?#code/KYDwDg9gTgLgBDAnmYcDKALAhlYATAQQAUBJIqCMAZzgF44BvAKDjgEs8AuOKmKNgHYBzANxMAvmKahIsBMlQAhAK4wYEAcTIVqddNlyFS5SjQBkjFvJTcARGBxYhULGAwBadwCNV6gbbFWADM2YAAbPAB9GDYYMOBuXn5hMUkmaXBoeCQUdDCOYC0TXXpMHHwinXNLVhyEuHtHZ1cPdyp8vGAAqxDwqPaCqm4AbQBdCSkZLOtULDCwggA3LDYwrC94gGEIAFtIAWABGBp6FTUNStM4AB88gsvqKQzZbIU4M79ik5qEWPjEviCUQSdJTOR1O6dL56ZisAadIZwMapOCgzLgt5zMKbeJYA54aGnXwaaG3NAdYBfSbo+AAYw0vAQEBxwDxhLgAAp6XsNIcYNwsUsVmsNsBtjyDkcqABKAXzFl4-CEgB8P3pAkZDBmcHEem5+z5gVRrHVjPVi2AsCVVTl2Nx+PZDF1WBoWIVDqqUjhAHdYrSMJy6tKfiaXahGi5mm5PD5zv5OFZWCaNBarQSqgA6GJxVD0fW8o4Z3oRaJ-YBGpNwLy4LAAawrcFpYYaDkjLmjbQptgTleTAlTMGtpgz8OA33zkpgRdCJdHVAbrGrrPrVnE6VYuBgyigAkbKctg-TplSQA

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The fundamental issue is that narrowing the type of component doesn't narrow the type of convertedProps, so TypeScript doesn't know that the assignments are okay.

You can fix it by creating componentProps in the branches:

export const toCleanProps = (component: allAvailableComponents): allCleanedProps => {
  const { type } = component;

  let convertedProps: allCleanedProps;

  switch (type) {
    case "paragraph--button":
      convertedProps = {
        title: component.field_title,
      };
      break;
    case "paragraph--slide":
      convertedProps = {
        slides: component.field_slides,
      };
      break;
    default:
      throw new Error(`Unexpected 'type'`);
  }

  return convertedProps;
}

Playground link

If the cleanup process is involved, you could split it off into helper functions:

const cleanButtonProps = (component: ButtonAPIProps): ButtonProps => {
  return {
    title: component.field_title,
  };
};
const cleanSlideProps = (component: SlideAPIProps): SlideProps => {
  return {
    slides: component.field_slides,
  };
};

export const toCleanProps = (component: allAvailableComponents): allCleanedProps => {
  const { type } = component;

  let convertedProps: allCleanedProps;

  switch (type) {
    case "paragraph--button":
      convertedProps = cleanButtonProps(component);
      break;
    case "paragraph--slide":
      convertedProps = cleanSlideProps(component);
      break;
    default:
      throw new Error(`Unexpected 'type'`);
  }

  return convertedProps;
}

Playground link

about 4 years ago · Santiago Trujillo 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