Skip to main content



function getClass(
| mongoose.Document
| { typegooseName: () => string }
| { typegooseName: string }
| string
| any
): NewableFunction | undefined


input RequiredanyA Input to get a name from

getClass is used to get a Class from a variety of inputs, which include:

  • mongoose.Document; Get the name to look-up from doc.constructor.modelName, only applies if no typegooseName function or property is present
  • { typegooseName: () => string }: Get the name to look-up from the typegooseName function (currently automatically added by typegoose)
  • { typegooseName: string }: Get the name to look-up from the typegooseName getter / property.
  • string: Directly specify the name to look-up.
  • any: Try to get a Class from any of the above, without having proper types.

This look-up only works if the class has been correctly registered with addModelToTypegoose (automatically done when calling getModelForClass or getDiscriminatorModelForClass, but not for buildSchema).


Will throw a Error when caching is disabled E033.


Any class that got compiled with buildSchema (transparently used by getModelForClass or getDiscriminatorModelForClass) a typegooseName function gets automatically added. This even makes it possible to get the class from sub-documents / sub-classes.


// Nested-GetClass & Document-GetClass
class Food {
public name: string;

class Kitten {
public currentFood: Food;

const KittenModel = getModelForClass(Kitten);

const input = new KittenModel();

// Nested
getClass(input.currentFood) === Food; // should be "true"
// Document
getClass(input) === Kitten; // should be "true"
// by string
getClass('Kitten') == Kitten; // should be "true"