microsoft / microsoft/TypeScript

Typescript fails to resolve module when type definitions are placed outside the root path (with typesVersions)

Open
#43,133 1 comment 2 reactions 1 assignee View on GitHub

@rbuckton is already working on this.

Since Mar 9, 2021.

Needs Investigation Rescheduled
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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';

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.