Reputation: 141
I using the CRA + Typescript to set up my project. And I also config the paths
property in tsconfig
file to use the absolute path.
Seems everything works fine after my configuration. But when I start my App using npm run start
or run test. I find the paths
property will be automatically removed.
here is the tsconfig.json
file I configured.
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react",
"baseUrl": ".",
"paths": {
"src/*": ["./src/*"]
}
},
"include": [
"src"
]
}
and I try to use like this way in the test file:
import i18n from 'src/i18n/mocks';
Is there something wrong with my configuration? can someone help me? thks.
Upvotes: 14
Views: 4669
Reputation: 155
There's a workaround.
baseUrl
config and paths
in there. Those won't be overwritten by CRA.{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"src/*": ["./src/*"]
}
}
}
{
"extends": "./base-tsconfig.json"
}
Read more here https://github.com/facebook/create-react-app/issues/5585
Upvotes: 5
Reputation: 127
Remove paths and let the baseUrl as src.
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react",
"baseUrl": "src"
},
"include": [
"src"
]
}
In that way, you will have the desired experience.
// file is inside src folder
import path from 'file'
Upvotes: -2