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 fails to resolve module when type definitions are placed outside the root path (with typesVersions) #43133

Open
pedrodurek opened this issue Mar 8, 2021 · 1 comment
Assignees
Labels
Needs Investigation This issue needs a team member to investigate its status. Rescheduled This issue was previously scheduled to an earlier milestone

Comments

@pedrodurek
Copy link

Bug Report

If you have a project with embedded type definitions that are not in root path. i.e.: src/index.d.ts and src/ts4.1/index.d.ts, TypeScript fails to resolve this module when using the following approach:

  "types": "./src/index.d.ts",
  "typesVersions": {
    ">=4.1": {
      "*": [
        "src/ts4.1/*"
      ]
    }
  },

src/i18n/config.ts:4:34 - error TS2307: Cannot find module 'react-i18next' or its corresponding type declarations.

import { useTranslation } from 'react-i18next';

It works, if we replace * to index.d.ts, like this:

  "types": "./src/index.d.ts",
  "typesVersions": {
    ">=4.1": {
      "*": [
        "src/ts4.1/index.d.ts"
      ]
    }
  },

However, when we replace it with the actual file (src/ts4.1/index.d.ts), VS Code sometimes imports the module incorrectly, i.e.:

import { useTranslation } from 'react-i18next/*';

Rather than:

import { useTranslation } from 'react-i18next';

Here you can find some context.

💻 Code

https://github.com/i18next/react-i18next/blob/master/package.json#L7

"types": "./src/index.d.ts",
"typesVersions": {
  ">=4.1": {
    "*": [
      "src/ts4.1/index.d.ts"
    ]
  }
},

🙁 Actual behavior

It fails to compile, and fails to auto-import properly.

import { useTranslation } from 'react-i18next/*';

🙂 Expected behavior

Resolve the module and import it properly.

import { useTranslation } from 'react-i18next';
@pedrodurek pedrodurek changed the title Typescript fails to resolve module when type definitions are declared outside the root path (with typesVersions) Typescript fails to resolve module when type definitions are placed outside the root path (with typesVersions) Mar 8, 2021
@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Mar 9, 2021
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.3.1 milestone Mar 9, 2021
@RyanCavanaugh RyanCavanaugh added the Rescheduled This issue was previously scheduled to an earlier milestone label Jun 18, 2021
@ReDrUm
Copy link

ReDrUm commented Oct 31, 2022

As a workaround, adding both * and an explicit index.d.ts seems to work for resolving root and nested entry point types for compilation and VSCode resolution:

"typesVersions": {
    ">=4.1": {
      "*": [
        "src/ts4.1/*",
        "src/ts4.1/index.d.ts"
      ]
    }
  },

RomainMuller added a commit to aws/jsii-compiler that referenced this issue Mar 30, 2023
The `typesVersions` directive applies recursively when the TypeScript
compiler searches for the appropriate object to load... When attempting
to resolve a directory, it will first rewrite that directory according
to the typesVersions directive, and then it will rewrite the `index`
sub-path within there again, resulting in two distinct rewrites
happening within the same resolution. This appears to be a TypeScript
bug as reported in microsoft/TypeScript#43133.

The work-around is to include a second rewrite candidate that explicitly
targets the `*/index.d.ts` sub-path, which makes removes the need for a
rewrite when searching down the `index` route.

Causes projen/projen#2570
aws-cdk-automation pushed a commit to aws/jsii-compiler that referenced this issue Mar 30, 2023
The `typesVersions` directive applies recursively when the TypeScript
compiler searches for the appropriate object to load... When attempting
to resolve a directory, it will first rewrite that directory according
to the typesVersions directive, and then it will rewrite the `index`
sub-path within there again, resulting in two distinct rewrites
happening within the same resolution. This appears to be a TypeScript
bug as reported in microsoft/TypeScript#43133.

The work-around is to include a second rewrite candidate that explicitly
targets the `*/index.d.ts` sub-path, which removes the need for a
rewrite when searching down the `index` route.

Causes projen/projen#2570



---

By submitting this pull request, I confirm that my contribution is made
under the terms of the [Apache 2.0 license].

[Apache 2.0 license]: https://www.apache.org/licenses/LICENSE-2.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Investigation This issue needs a team member to investigate its status. Rescheduled This issue was previously scheduled to an earlier milestone
Projects
None yet
Development

No branches or pull requests

5 participants