2016-11-13 128 views
1

我正在嘗試使用TypeScript和React Native。rootDir和outDir的意外行爲

這是我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 
} 

我想所有的.ts(X)在src目錄中的文件被編譯到構建目錄。

預計:

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

了:

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

編譯器會抱怨:

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. 

我在做什麼錯在這裏?

預先感謝您!

+0

是'[...]單曲不同(具體地具有不同的情況下)?我在我的Mach上看到類似錯誤的錯誤是:錯誤TS6059:File'/ Users/[...]'不在'rootDir'/ users/[...]'下。 'rootDir'預計包含所有源文件。一個是'/ Users',另一個是'/ users'。 – Gingi

回答

1

我偶然發現了同樣的問題。我認爲這發生在你用react-native-cli初始化一個項目並用react-native run-ios/run-android測試項目時。然後開始將項目轉換爲反應本機打字稿項目。這會在稍後創建「正常」反應原生構建工件和打字稿構建工件。

對我來說,它有助於將__tests__放入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 

}