Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

will remove paths in tsconfig automatically when I start the app

Tags:

typescript

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.

like image 627
Yaohan Hu Avatar asked Dec 02 '19 02:12

Yaohan Hu


People also ask

How does Tsconfig work?

The tsconfig. json file specifies the root files and the compiler options required to compile the project. JavaScript projects can use a jsconfig. json file instead, which acts almost the same but has some JavaScript-related compiler flags enabled by default.

What is include and exclude in Tsconfig?

The include and exclude properties take a list of glob-like file patterns. The supported glob wildcards are: * matches zero or more characters (excluding directory separators) ? matches any one character (excluding directory separators)

What is strict true in Tsconfig?

TypeScript's strict mode parameter can be configurated as several individual parameters for each specific case of type checking. So, basically, if you set the parameter strict to true in tsconfig. json it means that all these strict options are set to true.

What is Lib in Tsconfig json?

with --lib you can specify a list of built-in API declaration groups that you can chose to include in your project. For instance, if you expect your runtime to have support for Map, Set and Promise (e.g. most evergreen browsers today), just include --lib es2015.


1 Answers

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

like image 168
Bohdan Ivanchenko Avatar answered Sep 20 '22 15:09

Bohdan Ivanchenko