打字稿更喜欢导入相对导入而不是路径别名

发布于 2025-01-24 22:13:28 字数 900 浏览 0 评论 0原文

是否有一种方法可以强迫TS使用路径别名作为导入,如果有可用的话? (我使用VSCODE)

import { ApiError } from '../../../../libs/shared/src'; // This is imported by default
//import { ApiError } from '@rita/shared'; // I want this


const err: ApiError = { /* ... */ };

TS CONFIG提取

{
    "compilerOptions": {
        "rootDir": ".",
        "baseUrl": ".",
        "allowSyntheticDefaultImports": true,
        "target": "ES2017",
        "module": "esnext",
        "moduleResolution": "node",
        "forceConsistentCasingInFileNames": true,
        "importHelpers": true,
        "paths": {
            "@rita/helpers": ["libs/helpers/src/index.ts"],
            "@rita/maps": ["libs/maps/src/index.ts"],
            "@rita/rxjs": ["libs/rxjs/src/index.ts"],
            "@rita/shared": ["libs/shared/src/index.ts"]
        }
    }
}

Is there a way to force TS to use a path alias for imports if there is one available? (I use VSCode)

import { ApiError } from '../../../../libs/shared/src'; // This is imported by default
//import { ApiError } from '@rita/shared'; // I want this


const err: ApiError = { /* ... */ };

Ts config extract

{
    "compilerOptions": {
        "rootDir": ".",
        "baseUrl": ".",
        "allowSyntheticDefaultImports": true,
        "target": "ES2017",
        "module": "esnext",
        "moduleResolution": "node",
        "forceConsistentCasingInFileNames": true,
        "importHelpers": true,
        "paths": {
            "@rita/helpers": ["libs/helpers/src/index.ts"],
            "@rita/maps": ["libs/maps/src/index.ts"],
            "@rita/rxjs": ["libs/rxjs/src/index.ts"],
            "@rita/shared": ["libs/shared/src/index.ts"]
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

拿命拼未来 2025-01-31 22:13:28

我遇到了同样的问题,并在此GitHub问题之后修复了 https://github.com/microsoft.com/microsoft/microsoft/vscscode /eskoes/59815

在Visual Studio代码中,转到设置通过键入 ctrl +,(Windows/linux)或 cmd +,(MAC),然后使用搜索设置框,搜索ImportModudeSpecifier,然后从Uxpescript部分中选择非相关性。

I had the same problem and fixed it following this github issue https://github.com/Microsoft/vscode/issues/59815

Inside Visual Studio Code, go to Settings by typing Ctrl + , (Windows/Linux) or CMD + , (Mac), then using the Search settings box, search for importModuleSpecifier, then select from TypeScript section non-relative.

enter image description here

情释 2025-01-31 22:13:28

我在VScode中遇到了这个问题。问题是我已经将importmodulespecifier设置为settings.json中的相对。将其设置为默认解决了我的问题。

{
   // remove this or set to "shortest"
  "typescript.preferences.importModuleSpecifier": "relative"
}

I've had this issue in VSCode. The problem was I have set importModuleSpecifier to relative in settings.json. Setting this to default fixed my issue.

{
   // remove this or set to "shortest"
  "typescript.preferences.importModuleSpecifier": "relative"
}
绳情 2025-01-31 22:13:28

对我来说,这是一个完美的解决方案:

"typescript.preferences.importModuleSpecifier": "project-relative"

这允许在项目中相对导入,但是对于任何MonorePo共享的东西,我都会在tsconfig.json中获得我设置的路径。

For me, this was the perfect solution:

"typescript.preferences.importModuleSpecifier": "project-relative"

This allows for relative imports in a project, but for any monorepo shared stuff I get the path I set up in my tsconfig.json

蓦然回首 2025-01-31 22:13:28

我也有同样的问题,我认为这个问题与此 https://github.com /Microsoft/typeScript/essugy/47053

对我来说,绝对导入,例如。 @/组件/始终是第二个选项,无论我选择哪种设置。

这里提到的解决方案 https://stackoverflow.com/a/a/72029899 想更改我的index.ts导入/导出的任何内容。

似乎有帮助的是设置使用./而不是@/并使用“ typecript.preferences.importmodulespecifier”:“非权利” 在VSCODE中设置,但是感觉真的很奇怪,因为它看起来像是一条相对路径。

I have exactly the same problem and I think this issue is related to this https://github.com/microsoft/TypeScript/issues/47053.

For me, the absolute import, eg. @/components/ is always second option, no matter which setting I choose.

The solution mentioned here https://stackoverflow.com/a/72029899 did not work for me as well, and I also don't want to change anything about my index.ts import/exports.

What seems to help is to set paths to use ./ instead of @/ and use "typescript.preferences.importModuleSpecifier": "non-relative" setting in VSCode, but it feels really strange since it looks like a relative path.

孤独陪着我 2025-01-31 22:13:28

对于PATHS中的配置对象,请尝试以下操作:

{
  "@rita/helpers/*": ["libs/helpers/src/*"],
  "@rita/maps/*": ["libs/maps/src/*"],
  "@rita/rxjs/*": ["libs/rxjs/src/*"],
  "@rita/shared/*": ["libs/shared/src/*"]
}

也许直接引用index.ts不正确,因为它不是路径,而是文件。通配符也可能很重要。

仅供参考:我正在引用 tsconfig参考

For the config in paths object, try this:

{
  "@rita/helpers/*": ["libs/helpers/src/*"],
  "@rita/maps/*": ["libs/maps/src/*"],
  "@rita/rxjs/*": ["libs/rxjs/src/*"],
  "@rita/shared/*": ["libs/shared/src/*"]
}

Perhaps referencing the index.ts directly isn't right, as it's not a path, but a file. And the wildcard might be important too.

FYI: I'm referencing the TSConfig Reference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文