PropOptions causing Typescript-Error (TS2345)
See original GitHub issueI’m submitting a…
[ ] Regression
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
Current behavior
Currently with the latest release of @nestjs/mongoose
(7.2.2) Typescript no longer compiles any schema object that has properties with the @Prop
decorator with a PropOptions
object.
For example this code snippet (taken from the official NestJS documentation) https://docs.nestjs.com/techniques/mongodb
import {Prop, Schema} from "@nestjs/mongoose";
@Schema()
export class Cat {
@Prop({ required: true })
name: string;
}
will generate the following Typescript transpiling error:
error TS2345: Argument of type '{ required: true; }' is not assignable to parameter of type 'PropOptions'.
Type '{ required: true; }' is not assignable to type '"number"'.
19 @Prop({required: true})
~~~~~~~~~~~~~~~~
Package.json
"dependencies": { "@nestjs/common": "^7.5.1", "@nestjs/config": "^0.6.3", "@nestjs/core": "^7.5.1", "@nestjs/jwt": "^7.2.0", "@nestjs/mongoose": "^7.2.0", "@nestjs/passport": "^7.1.5", "@nestjs/platform-express": "^7.5.1", "@nestjs/swagger": "^4.7.12", "bcrypt": "^5.0.0", "class-transformer": "^0.3.2", "class-validator": "^0.13.1", "compression": "^1.7.4", "helmet": "^4.4.1", "mongoose": "^5.11.15", "nestjs-redoc": "^2.1.1", "passport-jwt": "^4.0.0", "reflect-metadata": "^0.1.13", "rimraf": "^3.0.2", "rxjs": "^6.6.3", "swagger-ui-express": "^4.1.6" }, "devDependencies": { "@nestjs/cli": "^7.5.1", "@nestjs/schematics": "^7.1.3", "@nestjs/testing": "^7.5.1", "@types/bcrypt": "^3.0.0", "@types/compression": "^1.7.0", "@types/express": "^4.17.8", "@types/helmet": "^4.0.0", "@types/jest": "^26.0.15", "@types/node": "^14.14.6", "@types/passport-jwt": "^3.0.4", "@types/supertest": "^2.0.10", "@typescript-eslint/eslint-plugin": "^4.6.1", "@typescript-eslint/parser": "^4.6.1", "eslint": "^7.12.1", "eslint-config-prettier": "7.0.0", "eslint-plugin-prettier": "^3.1.4", "jest": "^26.6.3", "prettier": "^2.1.2", "supertest": "^6.0.0", "ts-jest": "^26.4.3", "ts-loader": "^8.0.8", "ts-node": "^9.0.0", "tsconfig-paths": "^3.9.0", "typescript": "^4.0.5" },
I have an older NestJS project which is still compiling and uses the @nestjs/mongoose
package in version 7.0.2
. In this project the PropOptions
type is declared in the following way (which causes no errors):
export declare type PropOptions = mongoose.SchemaTypeOpts<any> | mongoose.Schema | mongoose.SchemaType;
In the current version 7.2.2
of @nestjs/mongoose
the PropOptions
is declared in this way (which is causing the Typescript-Error):
export declare type PropOptions = mongoose.SchemaDefinition['string'] | mongoose.SchemaType;
Expected behavior
No Typescript transpiling error occurs.
Minimal reproduction of the problem with instructions
https://github.com/SvenTiigi/nestjs-mongoose-issue-752.git
or:
Generate a new NestJS Project and add the @nestjs/mongoose
package:
$ nest new test-project
$ cd test-project
$ npm i --save @nestjs/mongoose mongoose
add the following code snippet to app.module.ts
import {Prop, Schema} from "@nestjs/mongoose";
@Schema()
export class Cat {
@Prop({ required: true })
name: string;
}
Run npm start
Environment
Nest version: 7.5.4
For Tooling issues:
- Node version: v15.7.0
- Platform: Mac
Others:
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:8 (6 by maintainers)
If we can avoid
any
we should. If we need it, we should probably useunknown
insteadFixed in v7.2.3