最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Typescript paths not resolving - Stack Overflow

programmeradmin1浏览0评论

Here's the Github MCVE showing an issue. npm run pile shows the error.

I'm trying to do this:

import {Todo} from '@test';

But it's not resolving.

src/index.ts:1:20 - error TS2307: Cannot find module '@test'.

I have paths in tsconfig.json.

  "baseUrl": "./",                          /* Base directory to resolve non-absolute module names. */
  "paths": {
    "@fs/": ["src/"], 
    "@test/": ["test/"]
  },                                        /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */

Thoughts?

Github Typescript Issue

I opened a bug report for this here since per some of the ments it should have worked.

Typescript note that I updated the github repository removing the glob patterns and slashes in @test.

Here's the Github MCVE showing an issue. npm run pile shows the error.

I'm trying to do this:

import {Todo} from '@test';

But it's not resolving.

src/index.ts:1:20 - error TS2307: Cannot find module '@test'.

I have paths in tsconfig.json.

  "baseUrl": "./",                          /* Base directory to resolve non-absolute module names. */
  "paths": {
    "@fs/": ["src/"], 
    "@test/": ["test/"]
  },                                        /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */

Thoughts?

Github Typescript Issue

I opened a bug report for this here since per some of the ments it should have worked.

Typescript note that I updated the github repository removing the glob patterns and slashes in @test.

Share Improve this question edited Mar 27, 2019 at 19:15 Ole asked Mar 27, 2019 at 18:10 OleOle 47.1k69 gold badges237 silver badges442 bronze badges 4
  • In the docs under path mapping they have set the base url to just . – pmkro Commented Mar 27, 2019 at 18:18
  • @pmkro . and ./ resolve to the same directory, the contrary would be a TS implementation bug – Nino Filiu Commented Mar 27, 2019 at 18:20
  • @NinoFiliu I figured as much – pmkro Commented Mar 27, 2019 at 18:21
  • For those who are not using a bundler (CRA, webpack, Nextjs, etc) and are stuck with this problem, maybe this will help you: stackoverflow./a/73346203/8221175 – Victor Commented Aug 13, 2022 at 17:07
Add a ment  | 

2 Answers 2

Reset to default 8

Don't adjust the VS Code Import Module Specifier settings (as per some answers floating around). And don't remove the glob patterns. In fact add some more in:

 "baseUrl": "./",
  "paths": {
    "@fs/*": ["src/*"], 
    "@test/*": ["test/*"]
  },

Notice that the glob is in the key AND the value. This is hilariously hard to spot sometimes.

Where the target is a file, the glob should be omitted from key and value.

Remove the glob patterns:

"paths": {
  "@test": "./test"
}

Your solution is weirdly supposed to work, but the docs use no globs and there have been some reported intellisense bugs when using globs.

发布评论

评论列表(0)

  1. 暂无评论