Yaohan Hu
Yaohan Hu

Reputation: 141

will remove paths in tsconfig automatically when I start the app

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

Answers (2)

Bohdan Ivanchenko
Bohdan Ivanchenko

Reputation: 155

There's a workaround.

  1. Create a new file "base-tsconfig.json" and put the baseUrl config and paths in there. Those won't be overwritten by CRA.
{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "src/*": ["./src/*"]
    }
  }
}
  1. Extend the main tsconfig.json file with your custom configuration
{
  "extends": "./base-tsconfig.json"
}

Read more here https://github.com/facebook/create-react-app/issues/5585

Upvotes: 5

greguintow
greguintow

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

Related Questions