question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PropOptions causing Typescript-Error (TS2345)

See original GitHub issue

I’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:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
jmcdo29commented, Feb 4, 2021

If we can avoid any we should. If we need it, we should probably use unknown instead

1reaction
kamilmysliwieccommented, Feb 5, 2021

Fixed in v7.2.3

Read more comments on GitHub >

github_iconTop Results From Across the Web

Vue&TypeScript: how to avoid error TS2345 when import ...
Error Message. At first I didn't really understand why TypeScript mentioning VueClass<Vue> and complaining about the template property. However, ...
Read more >
How to fix error TS2345: Argument of type 'Event' is not ...
Solution. This is due to the behavior of the --strictFunctionTypes compiler flag added in TypeScript v2. 6. A function of type (e: CustomEvent)...
Read more >
Vue + typescript - TypeError: Object prototype may only be an ...
The problem seems to be how Vue2 exposes itself, so import Vue from "vue" causes this error. I fixed it by using 'vue-property-decorator'...
Read more >
TypeScript with Options API - Vue.js
If your TypeScript version is less than 4.7 , you have to be careful when using function values for validator and default prop...
Read more >
Argument type 'unknown' is not assignable parameter of type
The error "Argument of type 'unknown' is not assignable to parameter of type" ... assignable // to parameter of type 'string'.ts(2345) getMessage(message);.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found