为了避免在基于 TypeScript 的 React Native 应用程序中出现“../../../../”样式的相对导入,我想配置该应用程序,以便我可以使用绝对导入。
重要的是配置还支持 Jest 单元测试。
我创建的应用程序使用 npx react-native init MyTestApp --template typescript
反应本机版本:0.60.5
我需要实现这一目标的确切配置是什么?
我正在使用create-react-app+ typescript,并且我想添加绝对路径。
我试图达到可以使用绝对路径的程度,如下所示:
代替import x from '../../../components/shell/shell'
使用import x from '@components/shell/shell';
这是tsconfig.json文件:
{
"extends": "./paths.json",
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"baseUrl": "src",
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react"
},
"include": [
"src"
]
}
Run Code Online (Sandbox Code Playgroud)
我使用扩展文件作为路径,因为由于某种原因npm start覆盖了该文件。paths.json文件也是如此:
{
"compilerOptions": {
"baseUrl": "./src",
"paths": {
"@": ["./src"],
"@components/*": …Run Code Online (Sandbox Code Playgroud)