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

TypeScript Build error: Cannot find type definition file for 'mongoose/types/pipelinestage' #11468

Closed
Davies-Owen opened this issue Mar 2, 2022 · 1 comment · Fixed by #11469
Assignees
Milestone

Comments

@Davies-Owen
Copy link

Davies-Owen commented Mar 2, 2022

Do you want to request a feature or report a bug?
Bug

What is the current behavior?
In a TypeScript project that uses a package that imports Mongoose, these errors will occur on build in a case-sensitive OS (e.g. Linux):

TS2688: Cannot find type definition file for 'mongoose/types/pipelinestage'.
TS2688: Cannot find type definition file for 'mongoose/types/error'.
TS2688: Cannot find type definition file for 'mongoose/types/connection'.

If the current behavior is a bug, please provide the steps to reproduce.
I'm working on a minimal repro, I will post it here when I'm done.

Here is what I think is going on:
#11368, #11418, #11369 and split index.d.ts into separate PipelineStage, Connection, and Error type declaration files. These are referenced in index.d.ts with triple-slash directives:

/// <reference path="./Error.d.ts" />
/// <reference path="./PipelineStage.d.ts" />
/// <reference path="./Connection.d.ts" />

When Typescript generates a type declaration file example-package.d.ts for my package that imports Mongoose, it adds this to the top of the file:

/// <reference types="mongoose/types/PipelineStage" />
/// <reference types="mongoose/types/Error" />
/// <reference types="mongoose/types/Connection" />
/// <reference types="mongoose" />

Now, when I attempt to use example-package in another project, a bug In TypeScript (microsoft/TypeScript#45096) causes paths in triple-slash directives to be lowercased. This mean that the compiler sees

/// <reference types="mongoose/types/PipelineStage" />

and looks for a declaration file at node_modules/mongoose/types/pipelinestage.d.ts. Since the real declaration file is PipelineStage.d.ts, on case-sensitive systems the compiler does not find any declaration file and throws an error. On case-insensitive systems the compiler finds the declaration file despite searching with the wrong case.
To work around the Typescript bug, I recommend using lower-case filenames for type declaration files: error.d.ts, pipeline-stage.d.ts, and connection.d.ts. I have tries this locally and confirmed that it fixes my project.

Here is a similar issue that was traced to the above TypeScript bug: microsoft/TypeScript#45509

tsconfig.json:

{
  "compilerOptions": {
    "baseUrl": ".",
    "outDir": "dist",
    "rootDir": "."
    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    "declaration": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "incremental": true,
    "jsx": "react",
    "lib": ["ES2020"],
    "module": "commonjs",
    "moduleResolution": "node",
    "pretty": true,
    "removeComments": true,
    "resolveJsonModule": true,
    "sourceMap": true,
    "target": "ES2020",
    "traceResolution": false
  }
  "include": ["src"]
}

Note: I have tried using the forceConsistentCasingInFileNames option. It has no effect on this issue.

What is the expected behavior?
Projects that depend on packages that depend on Mongoose should compile without errors on a case-sensitive OS.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node: v14.18.2
Mongoose: v6.2.4 (issue is present in versions ^6.2.2)
MongoDB: 4.3.1
TypeScript: 4.4.3

@Uzlopak
Copy link
Collaborator

Uzlopak commented Mar 2, 2022

Ok, this was actually some unexpected behaviour. I created a PR which renames filenames of the definition files to lowercase variants. #11469

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants