Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

112
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!