API Reference / @evolu/common / Type / recursive

Function: recursive()

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

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

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),
    }),
);

Type Parameters

Type Parameter
ParentType extends AnyType

Parameters

ParameterType
create() => ParentType

Returns

RecursiveType<ParentType>

Was this page helpful?