0

Let's say I have an interface

    export interface IAlert {
      cta?: CTA;
      description?: string;
      title?: string;
    }

How would you type this so that at least one property is passed in, but also allow for all three to be passed in.

I have tried...

export type TAlert = {
  type?: EAlertType;
} & (
  | {
      cta: CTA;
      description?: string;
      title?: string;
    }
  | {
      cta?: CTA;
      description: string;
      title?: string;
    }
  | {
      cta?: CTA;
      description?: string;
      title: string;
    }
);

However, I'm not happy with the error message it presents...

Type '{}' is not assignable to type 'IntrinsicAttributes & TAlert'.
  Property 'title' is missing in type '{}' but required in type '{ cta?: any; description?: string; title: string; }'

I'm unsure of how to either maintain the use of an interface and require a minimum of one property to be passed in or to use a type, but provide a more meaningful error.

Jared
  • 5,840
  • 5
  • 49
  • 83

1 Answers1

0

I can't help with the error messages but you could simplify:

export type TAlert = {
    type?: EAlertType;
    cta?: CTA;
    description?: string;
    title?: string;
  } & (
    | {
        cta: CTA;
      }
    | {
        description: string;
      }
    | {
        title: string;
      }
);
Aadmaa
  • 829
  • 5
  • 13