admin管理员组文章数量:1289543
Issue with Path Alias Resolution in TypeScript
I am encountering an issue when using path aliases in TypeScript.
When I import a module without explicitly including /index
, such as:
import { Colors } from "@utils";
or
import { Colors } from "@utils/";
I receive the error:
Cannot find module '@utils' or its corresponding type declarations.
However, if I add /index
explicitly, the import works fine:
import { Colors } from "@utils/index";
Strangely, when I don't want to add /index
explicitly, I have to navigate into a folder and then back out with /..
for the index
file to be detected, and everything works fine.
For example:
import { Colors } from "@utils/folder-example/..";
It seems like you have to at least navigate into one folder first, like adding @utils/folder-example/..
for the index to be detected. But when using just @utils
or @utils/
, the index file won't be detected.
This issue occurs with all the path aliases I have configured.
I have configured the path alias in my tsconfig.json
as follows:
{
"extends": "expo/tsconfig.base",
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"module": "CommonJS",
"moduleDetection": "force",
"moduleResolution": "node",
"strict": true,
"baseUrl": "./",
"paths": {
"@utils/*": [
"./src/utils/*"
]
}
}
}
Questions:
- Is this the expected behavior?
- How can I configure the path alias so that I don't need to add
/index
explicitly in the import?
I've tried various methods, but the results have been futile
Expected Behavior:
The import should resolve the path alias correctly without requiring the explicit addition of /index
. For example:
import { Colors } from "@utils";
This should work without any errors, just like importing with /index
explicitly. The path alias should automatically resolve to the index.ts
file in the specified directory, and no additional folder navigation should be necessary.
Issue with Path Alias Resolution in TypeScript
I am encountering an issue when using path aliases in TypeScript.
When I import a module without explicitly including /index
, such as:
import { Colors } from "@utils";
or
import { Colors } from "@utils/";
I receive the error:
Cannot find module '@utils' or its corresponding type declarations.
However, if I add /index
explicitly, the import works fine:
import { Colors } from "@utils/index";
Strangely, when I don't want to add /index
explicitly, I have to navigate into a folder and then back out with /..
for the index
file to be detected, and everything works fine.
For example:
import { Colors } from "@utils/folder-example/..";
It seems like you have to at least navigate into one folder first, like adding @utils/folder-example/..
for the index to be detected. But when using just @utils
or @utils/
, the index file won't be detected.
This issue occurs with all the path aliases I have configured.
I have configured the path alias in my tsconfig.json
as follows:
{
"extends": "expo/tsconfig.base",
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"module": "CommonJS",
"moduleDetection": "force",
"moduleResolution": "node",
"strict": true,
"baseUrl": "./",
"paths": {
"@utils/*": [
"./src/utils/*"
]
}
}
}
Questions:
- Is this the expected behavior?
- How can I configure the path alias so that I don't need to add
/index
explicitly in the import?
I've tried various methods, but the results have been futile
Expected Behavior:
The import should resolve the path alias correctly without requiring the explicit addition of /index
. For example:
import { Colors } from "@utils";
This should work without any errors, just like importing with /index
explicitly. The path alias should automatically resolve to the index.ts
file in the specified directory, and no additional folder navigation should be necessary.
1 Answer
Reset to default 0I've been facing the same issue since version 0.76 (I believe?)
My workaround is just to add another entry for each of them within tsconfig.json
:
{
"compilerOptions": {
"paths": {
"@utils": [
"./src/utils/index"
]
"@utils/*": [
"./src/utils/*"
]
}
}
}
本文标签: react nativePath Alias Not Resolving Correctly Without Explicit index in TypeScriptStack Overflow
版权声明:本文标题:react native - Path Alias Not Resolving Correctly Without Explicit index in TypeScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741458280a2379884.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论