drgross drgross - 16 days ago 7
TypeScript Question

Unexpected behaviour of rootDir and outDir

I am trying to use TypeScript with React Native.

This is my

tsconfig.json
:

{
"compilerOptions": {
"target": "es2015",
"module": "es2015",
"jsx": "react",
"outDir": "build",
"rootDir": "src",
"allowSyntheticDefaultImports": true,
"noImplicitAny": true,
"experimentalDecorators": true,
"preserveConstEnums": true,
"allowJs": true,
"sourceMap": true
},
"filesGlob": [
"typings/index.d.ts",
"src/**/*.ts",
"src/**/*.tsx"
],
"exclude": [
"index.android.js",
"index.ios.js",
"build",
"node_modules"
],
"compileOnSave": false
}


I would like all .ts(x) files in the src directory to be compiled to the build directory.

Expected:

MyAwesomeReactNativeApp/
├── src/
│ └── index.tsx
└── build/
└── index.js


Got:

MyAwesomeReactNativeApp/
├── src/
│ └── index.tsx
└── build/
├── src/
│ └── index.js
├── index.android.js
├── index.ios.js
└── __tests__/


The compiler complains:

error TS6059: File '[...]/__tests__/index.android.js' is not under 'rootDir' '[...]/src'. 'rootDir' is expected to contain all source files.
error TS6059: File '[...]/__tests__/index.ios.js' is not under 'rootDir' '[...]/src'. 'rootDir' is expected to contain all source files.
error TS6059: File '[...]/index.android.js' is not under 'rootDir' '[...]/src'. 'rootDir' is expected to contain all source files.
error TS6059: File '[...]/index.ios.js' is not under 'rootDir' '[...]/src'. 'rootDir' is expected to contain all source files.


What am I doing wrong here?

Thank you in advance!

sho sho
Answer

I stumbled across the same problem. I think it happens when you initialise a project with react-native-cli and test the project with react-native run-ios/run-android. And then start converting the project to a react-native typescript project afterwards. That creates 'normal' react-native build artifacts and typescript build artifacts later on.

For me it helped to put __tests__ into the excludes in tsconfig.json.

{
"compilerOptions": {
    "module": "es2015",
    "target": "es2015",
    "jsx": "react",
    "outDir": "build",
    "rootDir": "src",
    "allowSyntheticDefaultImports": true,
    "noImplicitAny": true,
    "experimentalDecorators": true,
    "preserveConstEnums": true,
    "allowJs": true,
    "sourceMap": true
},
"filesGlob": [
    "typings/index.d.ts",
    "src/**/*.ts",
    "src/**/*.tsx"
],
"exclude":[
    "index.android.js",
    "index.ios.js",
    "build",
    "node_modules",
    "__tests__"
],
"compileOnSave": false

}