RelationshipKeyField type

The key field that identifies a related entity in relationship inputs and results: id when the entity declares it, otherwise Id.

type RelationshipKeyField<T> = PrimaryKeyField extends keyof T
  ? "Id" extends keyof T
    ? {} extends Pick<T, PrimaryKeyField>
      ? [Required<T>[PrimaryKeyField]] extends [never]
        ? "Id"
        : PrimaryKeyField
      : PrimaryKeyField
    : PrimaryKeyField
  : "Id" extends keyof T
  ? "Id"
  : never