typegoose: InvalidTypeError: Type is invalid! Type is: "undefined"
Versions
- typegoose: 7.3.0
- mongoose: 5.9.24
Using the following definition for UserClass
:
@pre<UserClass>("save", async function (next: HookNextFunction): Promise<void> {
...
})
@index({ name: 1 }, { collation: { locale: "en", strength: 1 }, unique: true })
export class UserClass {
@prop({
required: true,
trim: true,
unique: true,
minlength: 3,
maxlength: 20
})
public name!: string;
...
}
I get the following unhelpful error:
@typegoose\typegoose\lib\internal\processProp.js:160
throw new errors_1.InvalidTypeError(name, key, Type);
^
InvalidTypeError: "UserClass.name"'s Type is invalid! Type is: "undefined"
at Object.processProp (C:\Users\winterNebs\source\repos\AscensionGame\AscensionGame\node_modules\@typegoose\typegoose\lib\internal\processProp.js:160:15)
at Object._buildSchema (C:\Users\winterNebs\source\repos\AscensionGame\AscensionGame\node_modules\@typegoose\typegoose\lib\internal\schema.js:37:27)
at buildSchema (C:\Users\winterNebs\source\repos\AscensionGame\AscensionGame\node_modules\@typegoose\typegoose\lib\typegoose.js:120:20)
at Object.getModelForClass (C:\Users\winterNebs\source\repos\AscensionGame\AscensionGame\node_modules\@typegoose\typegoose\lib\typegoose.js:70:39)
at Object../database/schema/user.ts (C:\Users\winterNebs\source\repos\AscensionGame\AscensionGame\dist\app.js:2121:28)
at __webpack_require__ (C:\Users\winterNebs\source\repos\AscensionGame\AscensionGame\dist\app.js:20:30)
at Object../gameAppServer.ts (C:\Users\winterNebs\source\repos\AscensionGame\AscensionGame\dist\app.js:2409:16)
at __webpack_require__ (C:\Users\winterNebs\source\repos\AscensionGame\AscensionGame\dist\app.js:20:30)
at Object../app.ts (C:\Users\winterNebs\source\repos\AscensionGame\AscensionGame\dist\app.js:1351:25)
at __webpack_require__ (C:\Users\winterNebs\source\repos\AscensionGame\AscensionGame\dist\app.js:20:30)
at C:\Users\winterNebs\source\repos\AscensionGame\AscensionGame\dist\app.js:84:18
at Object.<anonymous> (C:\Users\winterNebs\source\repos\AscensionGame\AscensionGame\dist\app.js:87:10)
at Module._compile (internal/modules/cjs/loader.js:955:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
at Module.load (internal/modules/cjs/loader.js:811:32)
at Function.Module._load (internal/modules/cjs/loader.js:723:14)
As far as I can tell the type for name
is string
, not undefined
.
Can anyone spot what I did wrong or is this error not related to this code?
I migrated from ts-mongoose since that project doesn’t look like its being updated anymore, so sorry that I don’t have a very minimal example.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 15 (6 by maintainers)
encountering the same issue right now. setting
"emitDecoratorMetadata": true
in the tsconfig.json fixed it for mefor people using babel instead of tsc, this plugin also fixes it
@aderchox if your stack-trace is anything to go by, then you are using
esbuild
, which does not support outputting decorator metadata because it does not have type information, see https://github.com/evanw/esbuild/issues/257#issuecomment-658053616your options are:
tsc
compiler for you esbuild chaintsc
directly@winterNebs there dosnt seem to be anything wrong with the provided code, please confirm the following: