gpt4 book ai didi

typescript - rootDir 和 outDir 的意外行为

转载 作者:搜寻专家 更新时间:2023-10-30 21:52:35 25 4
gpt4 key购买 nike

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

这是我的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
}

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

预期:

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.

我在这里做错了什么?

提前致谢!

最佳答案

我遇到了同样的问题。我认为当您使用 react-native-cli 初始化项目并使用 react-native run-ios/run-android 测试项目时会发生这种情况。然后开始将项目转换为 react-native typescript 项目。这会在稍后创建“正常的”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

关于typescript - rootDir 和 outDir 的意外行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40579191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com