Skip to main content

isRefType & isRefTypeArray

isRefType

Typings:

function isRefType<T, S extends RefType>(doc: Ref<T, S> | null | undefined, refType: AllowedRefTypes): doc is NonNullable<S>

Parameters:

NameTypeDescription
doc RequiredRef<T, S>The Document to check
refType RequiredAllowedRefTypesThe Expected Reference Type to test for

isRefType checks if the given Input (doc) is of the given Type (refType).
Option refType is required because the known Reference Type only exists at compile time, not at runtime so it needs to be explicitly defined (to have accurate checks).

Example

class Cat {
@prop({ ref: 'Cat' })
public partner: Ref<Cat>;

// this example could be smaller, but for demonstation purposes this is a longer version
public hasPartner(): boolean {
if (isRefType(this.partner, mongoose.Types.ObjectId)) {
// "this.partner" now has the type of "Cat._id"'s RefType (in this case "ObjectId")
return true;
} else {
return false;
}
}
}

-> this could be minified, but for demonstration purposes this will stay the long version

isRefTypeArray

Overload 1

Typings:

function isRefTypeArray<T, S extends RefType>(
docs: mongoose.Types.Array<Ref<T, S>> | undefined,
refType: AllowedRefTypes
): docs is mongoose.Types.Array<NonNullable<S>>;

Parameters:

NameTypeDescription
docs Requiredmongoose.Types.Array<Ref<T, S>>The Array of Documents to check
refType RequiredAllowedRefTypesThe Expected Reference Type to test for

Overload 2

Typings:

function isRefTypeArray<T, S extends RefType>(docs: Ref<T, S>[] | undefined, refType: AllowedRefTypes): docs is NonNullable<S>[];

Parameters:

NameTypeDescription
docs RequiredRef<T, S>[]The Array of Documents to check
refType RequiredAllowedRefTypesThe Expected Reference Type to test for

Description

isRefTypeArray checks if all the items in the given Array (docs) are matching the given Reference type (refType).
This function calls isRefType for each item in the array.
Only returns true if all items in the array return true.

Example

class Cat {
@prop({ ref: 'Cat' })
public kittens: Ref<Cat>;

// this example could be smaller, but for demonstation purposes this is a longer version
public areAllKittensExisting(): boolean {
if (isRefTypeArray(this.kittens, mongoose.Types.ObjectId)) {
// "this.kittens" now has the type of "Cat._id"'s RefType (in this case "ObjectId")
return true;
} else {
return false;
}
}
}

AllowedRefTypes

The Allowed Reference Types for isRefType and isRefTypeArray are:

  • String
  • Number
  • Buffer
  • mongoose.Types.Buffer
  • mongoose.Types.ObjectId