[英]React Native Typescript babel-plugin-module-resolver error: Cannot find module or its corresponding type declerations
I can use the module resolver with Javascript without any problem.我可以毫无问题地使用带有 Javascript 的模块解析器。 Actually I can use it with Typescript without problem on runtime but on developing part I could not find the solution of
Cannot find module or its corresponding type declerations
problem.实际上,我可以在运行时毫无问题地将它与 Typescript 一起使用,但在开发部分我找不到
Cannot find module or its corresponding type declerations
问题的解决方案。 I'm looking for an answer which part am I doing it wrong?我正在寻找答案,我做错了哪一部分?
Here are the files:以下是文件:
.babelrc .babelrc
{
"presets": ["module:metro-react-native-babel-preset"],
"plugins": [
[
"module-resolver",
{
"root": ["./src"],
"alias": [
{ "@shared-components": "./shared/components" },
{ "@shared-constants": "./shared/constants" },
{ "@shared-theme": "./shared/theme" },
{ "@font-size": "./shared/theme/font-size" },
{ "@api": "./services/api/index" },
{ "@fonts": "./shared/theme/fonts/index" },
{ "@colors": "./shared/theme/colors" },
{ "@theme": "./shared/theme/index" },
{ "@services": "./services" },
{ "@screens": "./screens" },
{ "@utils": "./utils/" },
{ "@assets": "./assets/" }
],
"extensions": [".js", ".jsx", ".ts", ".tsx"]
}
]
]
}
tsconfig.json配置文件
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"lib": ["esnext"],
"allowJs": true,
"jsx": "react-native",
"noEmit": true,
"isolatedModules": true,
"strict": true,
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
// ? Custom ones
"skipLibCheck": true,
"resolveJsonModule": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true,
"noImplicitThis": true,
"alwaysStrict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"forceConsistentCasingInFileNames": true,
// ? Babel Plugin Module Resolver
"baseUrl": "./src",
"paths": {
"@shared-components": ["./shared/components"],
"@shared-constants": ["./shared/constants"],
"@shared-theme": ["./shared/theme"],
"@font-size": ["./shared/theme/font-size"],
"@api": ["./services/api/index"],
"@fonts": ["./shared/theme/fonts/index"],
"@colors": ["./shared/theme/colors"],
"@theme": ["./shared/theme/index"],
"@services": ["./services"],
"@screens": ["./screens"],
"@utils": ["./utils/"],
"@assets": ["./assets/"]
}
},
"exclude": [
"node_modules",
"babel.config.js",
"metro.config.js",
"jest.config.js"
]
}
import HomeScreen from "@screens/home/HomeScreen";
import SearchScreen from "@screens/search/SearchScreen";
import DetailScreen from "@screens/detail/DetailScreen";
The error错误
Thank you for the helping :)谢谢你的帮助:)
The way of mapping paths
in your configuration doesn't look right.在您的配置中映射
paths
的方式看起来不对。 The right one is supposed to be:正确的应该是:
paths": {
"@screens/*": ["./screens/*"], // You need to specify prefix `/*` as well
// others ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.