Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add typesVersion with minimum TypeScript version #12529

Closed
2 tasks done
elszczepano opened this issue Oct 5, 2022 · 17 comments
Closed
2 tasks done

Add typesVersion with minimum TypeScript version #12529

elszczepano opened this issue Oct 5, 2022 · 17 comments
Labels
typescript Types or Types-test related issue / Pull Request

Comments

@elszczepano
Copy link

elszczepano commented Oct 5, 2022

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

6.6.5

Node.js version

16.16.0

MongoDB server version

6.0.2 (not required)

Description

I've experienced an issue with mongoose above version 6.6.3. When I try to build a project that uses mongoose in version 6.6.4 or higher I receive:

Screenshot 2022-10-05 at 20 58 43

When I switched to 6.6.3 the issue does not occur. My TS config:

{
  "compilerOptions": {
    "outDir": "./dist",
    "rootDir": "./src",
    "esModuleInterop": true,
    "lib": [
      "es2021",
      "dom"
    ],
    "module": "commonjs",
    "target": "es2021",
    "moduleResolution": "node",
    "allowJs": true,
    "noImplicitAny": true,
    "composite": true,
    "skipLibCheck": true,
    "sourceMap": true,
    "strictNullChecks": true,
    "experimentalDecorators": true,
    "forceConsistentCasingInFileNames": true,
    "include": [
        "src/**/*"
    ]
  }
}

As I briefly checked, the pointed places seem to be syntactically correct so the issue is a bit awkward for me. I'd be grateful for a hint if an issue is on my side.

Steps to Reproduce

Just run tsc --build for a project that uses mongoose.

Expected Behavior

The project is built correctly.

@vkarpov15
Copy link
Collaborator

@elszczepano what version of TypeScript? I'm able to run tsc --build with a slightly fixed version of your tsconfig.json with TypeScript 4.8.4.

Maybe because your tsconfig.json is invalid? You're missing a , after "esModuleInterop": true and include is not a valid compilerOptions property.

@vkarpov15 vkarpov15 added can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. typescript Types or Types-test related issue / Pull Request labels Oct 5, 2022
@ultrarunner
Copy link

ultrarunner commented Oct 6, 2022

I have a similar issue as @elszczepano with typescript 4.3.4, node 14.19. Just wanted to add some context. I am on mongoose@6.5.2 and was trying to upgrade to latest. Tried to downgrade to 6.6.3 without success. I cannot even upgrade to 6.5.3. Getting a compilation error as well there on a single line.
node_modules/mongoose/types/query.d.ts:619:29 - error TS2304: Cannot find name 'this'. 619 toConstructor(): typeof this; ~~~~ Found 1 error.
Looked at the differences between the 2 releases and noticed the query.d.ts file has a lot of updates. Hope this helps. Will keep trying to figure it out.

@elszczepano
Copy link
Author

elszczepano commented Oct 6, 2022

Maybe because your tsconfig.json is invalid? You're missing a , after "esModuleInterop": true and include is not a valid compilerOptions property.

Even after fixing the tsconfig the issue still occurs in my case. Regarding the TS version, I use 4.6.3.

EDIT:
I can confirm the issue does not occur on the latest TS version (4.8.4). Also 4.7.3 so the issue affects only TS in version 4.6 and below

@kYem
Copy link

kYem commented Oct 6, 2022

I had exactly same problem with TS 4.6.4, had to upgrade to latest version and it went away.

@hasezoey
Copy link
Collaborator

hasezoey commented Oct 7, 2022

Duplicate of #12512

@hasezoey hasezoey marked this as a duplicate of #12512 Oct 7, 2022
@hasezoey
Copy link
Collaborator

hasezoey commented Oct 7, 2022

@ultrarunner

node_modules/mongoose/types/query.d.ts:619:29 - error TS2304: Cannot find name 'this'. 619 toConstructor(): typeof this; ~~~~ Found 1 error.

this should be changed with #12522, which was included in mongoose 6.6.5

@ItsMorningstar
Copy link

mongoose@6.6.5
Typescript@4.8.4
node v18.9.0

Facing following query.d.ts errors.
image
Please help

@hasezoey
Copy link
Collaborator

hasezoey commented Oct 9, 2022

@YooItsMaster did you already try clearing the node modules and re-installing? also are you sure you are using typescript 4.8 to compile?

@ItsMorningstar
Copy link

Oh although i had ts v4.8.4 installed it was still using v4.6.2 for compilation. That fixed it. Sry, Thank

@AlexRMU
Copy link

AlexRMU commented Oct 9, 2022

Dude...
image

@hasezoey
Copy link
Collaborator

hasezoey commented Oct 9, 2022

@AlexRMU from what i can tell, the current types (typeof Query) is correct, because the function toConstructor is meant to return a Class (constructor) not a instance of a class, and because Query is a class, by default in typescript this means that the type is a instance of a class

class Query<ResultType, DocType, THelpers = {}, RawDocType = DocType> implements SessionOperation {

@hxtree
Copy link

hxtree commented Oct 20, 2022

Oh although i had ts v4.8.4 installed it was still using v4.6.2 for compilation. That fixed it. Sry, Thank

I ran into the same issue. I had Typescript 4.8.4 installed globally but in a monorepo package.json there was 4.6.2, which needed to change to.

  "devDependencies": {
    "typescript": "4.8.4"
  },

This is a really obscure requirement to find, Perhaps the Typescript minimum version could be placed README or similar to help distribute this info?

@hxtree
Copy link

hxtree commented Oct 22, 2022

It appears, if a typesVersions were added to mongoose / package.json, probably similar to:

  "typesVersions": {
    ">=4.8.4": { "*": "./types/index.d.ts"] }
  }

It would produce the following error using the package with a unsupported version. And the error message appears to be improvable.

"'package.json' does not have a 'typesVersions' entry that matches version '{0}'."

References:
https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html#version-selection-with-typesversions
microsoft/TypeScript#32166
https://github.com/search?q=%22%3E%3D4.8.4%22+filename%3Apackage.json+typesversions

@vkarpov15 vkarpov15 changed the title Incorrect types for query.d.ts in versions above 6.6.3 Add typesVersion with minimum TypeScript version Oct 25, 2022
@vkarpov15 vkarpov15 removed the can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. label Oct 25, 2022
@vkarpov15 vkarpov15 added this to the 6.8 milestone Oct 25, 2022
@sikmakn
Copy link

sikmakn commented Oct 31, 2022

I have the same problem. Typescript version 4.8.3

@Mankore
Copy link

Mankore commented Nov 1, 2022

Same issue

node: 'v16.15.1'
npm: 8.13.2
"mongoose": "^6.7.0"
"typescript": "^4.8.4"

using "skipLibCheck": true, in typescript config or upgrading to latest version of typescript doesn't work for me

@vkarpov15
Copy link
Collaborator

@sikmakn @Mankore please open a new issue and follow the issue template.

@Automattic Automattic locked as spam and limited conversation to collaborators Nov 9, 2022
@vkarpov15
Copy link
Collaborator

@hxtree I'm not getting any error when running with a TypeScript version that isn't listed in typesVersion, for example with the following:

"types": "./types/index.d.ts",
  "typesVersions": {
    ">=4.2": { "*": ["./types/index.d.ts"] }
  },

A sample Mongoose script compiles just fine with TypeScript 4.1. This looks to be expected behavior from TypeScript. For example, look at the following highlighted section in the TS docs you linked to.

image

@vkarpov15 vkarpov15 removed this from the 6.8 milestone Dec 2, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

No branches or pull requests

10 participants