Validation with Typescript, Ramda and "prop"

Viewed 44

I am trying to build some validation for a custom TS Type, like this:

export type Calendar = {
  active: boolean;
  businessDay: string[];
  displayName: string;
  holiday: Holiday[];
  label: string;
};

export const isValidCalendar = (calendar: Calendar) =>
  allPass([
    pipe(prop<string, string>('label'), isNil, not),
    pipe(prop<string, string>('displayName'), isNil, not),
  ])(calendar);

But apparently I cannot apply (calendar) to my allPass, due to this error:

Argument of type '[Calendar]' is not assignable to parameter of type 'Record<string, string> & Record<string, string[]>'. Type '[Calendar]' is not assignable to type 'Record<string, string>'. Index signature for type 'string' is missing in type '[Calendar]'.ts(2345)

Why is this happening? How can I solve it?

1 Answers

I solved it, by creating my own "Predicate" type, and adding it to allPass

type Pred<T> = (x: T) => boolean;

export const isValidCalendar = allPass<Pred<Calendar>>([
  pipe(prop('label'), isNil, not),
  pipe(prop('displayName'), isNil, not),
  pipe(prop('businessDay'), length, gt(__, 0)),
]);

Related