Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Make type accept a property + one of 4 sets

Here’s my code:

interface Props<T> {
  config: {
    title?: string,
    (
      limitText: boolean,
      bold: boolean,
      content: ReactNode
    )
    | (iterable: boolean, content: T[], selector: string)
    | (markup: boolean, content: string)
    | (stack: boolean, coloredLevel?: boolean, content: string[])
  }
}

What I’m trying to achieve is for config to accept title, and one of the other 4 types. How can I achieve this? I was thinking smth like discriminatory union type deal.

ts playground

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :


interface PropsBase<T> {
  config: {
    title?: string,
  }
}

interface Props1 extends PropsBase<unknown> {
    config: {
      title?: string,
      limitText: boolean,
      bold: boolean,
      content: 'ReactNode'
    }
}

interface Props2<T> extends PropsBase<T> {
    config: {
        title?: string,
        markup: boolean,
        content: T,
    }
    
}

interface Props3 extends PropsBase<unknown> {
    config: {
        title?: string
        stack: boolean,
        coloredLevel?: boolean,
        content: string[]
    }
}

type Props<T> = Props1 | Props2<T> | Props3;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading