rootDir 和 outDir 的意外行为

drg*_*oss 5 typescript react-native

我正在尝试将 TypeScript 与 React Native 结合使用。

\n\n

这是我的tsconfig.json

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

我希望将 src 目录中的所有 .ts(x) 文件编译到构建目录。

\n\n

预期的:

\n\n
MyAwesomeReactNativeApp/\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 src/\n\xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 index.tsx\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 build/\n    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 index.js\n
Run Code Online (Sandbox Code Playgroud)\n\n

得到:

\n\n
MyAwesomeReactNativeApp/\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 src/\n\xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 index.tsx\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 build/\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 src/\n    \xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 index.js\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 index.android.js\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 index.ios.js\n    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 __tests__/\n
Run Code Online (Sandbox Code Playgroud)\n\n

编译器抱怨:

\n\n
error TS6059: File \'[...]/__tests__/index.android.js\' is not under \'rootDir\' \'[...]/src\'. \'rootDir\' is expected to contain all source files.\nerror TS6059: File \'[...]/__tests__/index.ios.js\' is not under \'rootDir\' \'[...]/src\'. \'rootDir\' is expected to contain all source files.\nerror TS6059: File \'[...]/index.android.js\' is not under \'rootDir\' \'[...]/src\'. \'rootDir\' is expected to contain all source files.\nerror TS6059: File \'[...]/index.ios.js\' is not under \'rootDir\' \'[...]/src\'. \'rootDir\' is expected to contain all source files.\n
Run Code Online (Sandbox Code Playgroud)\n\n

我在这里做错了什么?

\n\n

先感谢您!

\n

小智 2

我偶然发现了同样的问题。我认为当您使用react-native-cli初始化项目并使用react-native run-ios/run-android测试项目时会发生这种情况。然后开始将项目转换为react-native typescript 项目。这会在稍后创建“正常”的反应本机构建工件和打字稿构建工件。

对我来说,将 __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
Run Code Online (Sandbox Code Playgroud)

}