API reference@evolu/commonType › recursive

function recursive<ParentType>(
  create: () => ParentType,
): RecursiveType<ParentType>;

Defined in: packages/common/src/Type.ts:3808

Recursive Type.

Recursive types can't be inferred, so we must define them using an interface and recursive Type Factory that returns a Type.

Example

interface Category {
  readonly name: string;
  readonly subcategories: ReadonlyArray<Category>;
}

interface CategoryInput {
  readonly name: string;
  readonly subcategories: ReadonlyArray<CategoryInput>;
}

type CategoryError = ObjectError<{
  readonly name: typeof String.Error;
  readonly subcategories: ArrayError<CategoryError>;
}>;

const Category = recursive(
  (): Type<"Object", Category, CategoryInput, CategoryError> =>
    object({
      name: String,
      subcategories: array(Category),
    }),
);