打字稿更喜欢导入相对导入而不是路径别名
是否有一种方法可以强迫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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我遇到了同样的问题,并在此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.我在VScode中遇到了这个问题。问题是我已经将
importmodulespecifier
设置为settings.json中的相对。将其设置为默认解决了我的问题。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.对我来说,这是一个完美的解决方案:
这允许在项目中相对导入,但是对于任何MonorePo共享的东西,我都会在
tsconfig.json
中获得我设置的路径。For me, this was the perfect solution:
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
我也有同样的问题,我认为这个问题与此 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.对于
PATHS中的配置
对象,请尝试以下操作:也许直接引用
index.ts
不正确,因为它不是路径,而是文件。通配符也可能很重要。仅供参考:我正在引用 tsconfig参考。
For the config in
paths
object, try this: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.