我收到了这个相当无意义的 tsc 转译错误:
错误 TS6059:文件“/Users/alex/codes/interos/teros-cli/src/logging.ts”不在“rootDir”“/Users/alex/codes/teros/notifier-server/src”下。'rootDir' 应包含所有源文件。
我的 PWD/Users/alex/codes/teros/notifier-server和 tsconfig.json 文件/Users/alex/codes/teros/notifier-server/tsconfig.json是:
{
"compilerOptions": {
"outDir": "dist",
"allowJs": false,
"pretty": true,
"resolveJsonModule": true,
"sourceMap": false,
"skipLibCheck": true,
"rootDir": "src",
"declaration": false,
"baseUrl": ".",
"target": "es2018",
"module": "commonjs",
"noImplicitAny": true,
"removeComments": true,
"allowUnreachableCode": true,
"lib": [
"es2017",
"es2018"
]
},
"compileOnSave": false,
"include": [
"src"
]
}
Run Code Online (Sandbox Code Playgroud)
这似乎是一个bug..since TEROS-CLI dir是PWD外,并通过单独的tsconfig.json文件管理。
我什至将此字段更改为:
"include": [
"/Users/alex/codes/teros/notifier-server/src"
],
"exclude": [
"/Users/alex/codes/teros/teros-cli"
]
Run Code Online (Sandbox Code Playgroud)
仍然得到同样的错误。
我正在为客户将一个大型的 monorepo 转换为 TypeScript,但是,我自己对 TS 还很陌生,并且遇到了一个错误,我找不到明显的修复方法。
TS6059: File '[path to repo root]/packages/config/globals.ts' is not under 'rootDir' '[path to repo root]/packages/components/src'. 'rootDir' is expected to contain all source files.
该globals.ts文件不应该存在于components包中,它属于config包,所以我不太明白这个错误。
我在存储库的根目录中有一个主 tsconfig 文件(https://github.com/serge-web/serge/blob/feature/333-game-admin-channel/tsconfig.json),然后每个包都有自己的tsconfig 文件扩展了该文件。该components包的位置在这里: https: //github.com/serge-web/serge/blob/feature/333-game-admin-channel/packages/components/tsconfig.json
我假设我错误地扩展了包中的 tsconfig 文件,或者我references错误地使用了它,但我找不到正确的方法来执行此操作。
如果您需要查看结构,这里是存储库的链接:https://github.com/serge-web/serge/tree/feature/333-game-admin-channel
在 TS 项目中,我希望阻止以下内容:
projectAprojectA我希望允许以下内容:
common。我知道参考文献。但是,据我了解,它们需要构建用于类型检查(如果进行了这种分离,则必须先构建以创建 d.ts 文件),我宁愿避免这种情况。
我有哪些选择?是否可以简单地通过每个项目/文件夹的单独 tsconfig 文件来实现?
我有一个具有以下结构的打字稿nodejs服务器:
tsconfig.json
package.json
src/
middleware/
utils/
index.ts
dist/
middleware/
utils/
index.ts
Run Code Online (Sandbox Code Playgroud)
使用Typescript 2时,我可以将项目从src /转换为dist /文件夹,并可以使用我的目录结构的镜像。
随着Typescript 3的发布,他们引入了项目参考,并更改了将代码转换为输出目录的方式。现在tsc以这种嵌套方式输出到dist /文件夹:
dist/
src/
middleware/
utils/
index.ts
Run Code Online (Sandbox Code Playgroud)
我的tsconfig.json是:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"allowJs": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"resolveJsonModule": true,
"declaration": false,
"outDir": "dist/",
"lib": [
"es7",
"dom"
]
},
"include": [
"src/"
]
}
Run Code Online (Sandbox Code Playgroud)
如何配置Typescript将src /文件夹作为镜像输出到dist /文件夹中?
给定一个 monorepo 项目。
--project_root/
|--packageA/
|--index.ts
|--package.json
|--foo/
|--index.ts
|--packageB/
|--index.ts
|--package.json
|--bar/
|--spam.ts
通常,当您进入packageA/index.ts并想要导入时,packageB/index.ts您会这样做import index from '../packageB',
当你packageA/foo/index.ts想要导入时,packageB/index.ts你需要向上移动两个目录import index from '../../packageB'
问题是,有没有办法像import index from 'packageB嵌套文件夹一样导入import spam from 'packageB/bar/spam'?
编辑
我已经上传了一个 github repo 来演示这个问题 https://github.com/jaimesangcap/lerna-ts-monorepo
安装后node-config和@types/config:
yarn add config
yarn add --dev @types/config
Run Code Online (Sandbox Code Playgroud)
并按照lorenwest / node-config中的描述添加配置:
// default.ts
export default {
server: {
port: 4000,
},
logLevel: 'error',
};
Run Code Online (Sandbox Code Playgroud)
当我尝试在我的应用中使用时:
import config from 'config';
console.log(config.server);
Run Code Online (Sandbox Code Playgroud)
我收到错误消息:
src/app.ts(19,53): error TS2339: Property 'server' does not exist on type 'IConfig'.
Run Code Online (Sandbox Code Playgroud) 我有一个像这样的文件夹结构:
- mono-repo
tsconfig.paths.json
- Website
tsconfig.json
- src
test.ts
index.ts
- Tool
- src
index.ts
Run Code Online (Sandbox Code Playgroud)
- mono-repo
tsconfig.paths.json
- Website
tsconfig.json
- src
test.ts
index.ts
- Tool
- src
index.ts
Run Code Online (Sandbox Code Playgroud)
// mono-repo/Website/src/index.ts
import { test } from "test";
import { tool } from "tool";
test(tool);
Run Code Online (Sandbox Code Playgroud)
我希望能够进行扩展,tsconfig.paths.json以便每个包都为其他包正确输入模块导入。
失败的尝试 1
// mono-repo/tsconfig.paths.json
{
"compilerOptions": {
"paths": {
"tool": ["Tool/src"],
}
}
}
Run Code Online (Sandbox Code Playgroud)
问题:找不到模块“工具”。添加到路径的 baseUrl 指向mono-repo/Website/src/Tool/src. 这不是一条真正的道路。
失败的尝试 2
// mono-repo/Website/src/index.ts
import { test } from "test"; …Run Code Online (Sandbox Code Playgroud) 我发现我的应用程序结构和构建过程中存在使用WebPack,TypeScript和TS-Loader的问题,我认为这是由TypeScript 2.1.4引起的,但显然是在那里.
您可以从我的其他帖子中看到所有详细信息: TypeScript 2.1.4打破了webpack ts-loader中的更改
简而言之,我将Gulp和WebPack设置为/client/app.ts的入口点,现在它几乎没有任何东西(当然没有引用/ server /)但是WebPack构建过程的TypeScript编译阶段仍在尝试在/ server上运行(在我的另一篇文章中,显示来自Server文件夹的编译错误,当它应该只从Client文件夹运行时).
我做错了什么以及如何修复它以便它只在/client/.ts文件上运行,特别是从app.ts中走出结构?
这是我的回购展示了我到目前为止所做的一切:https: //github.com/CmdrShepardsPie/test-ts-app/tree/develop
谢谢
我的包裹是这样的:
? tsconfig.json
? src/
? ? index.ts (import './dependence.ts')
? ? dependence.ts
? example/
? index.html
? script.ts (import '../src/index.ts')
Run Code Online (Sandbox Code Playgroud)
我想
./src/*.ts 编译在 ./dist/*.js./example/*.ts 编译在 ./example/*.js运行后tsc,我希望我的包看起来像这样:
? tsconfig.json
? src/
? ? index.ts (import './dependence.ts')
? ? dependence.ts
?!dist/
? ?!index.js (import './dependence.js')
? ?!dependence.js
? example/
? index.html
? script.ts (import '../src/index.ts')
?!script.js (import '../dist/index.js')
Run Code Online (Sandbox Code Playgroud)
我对所有 tsconfig 选项都有些困惑。
我已经尝试了很多选项,如baseUrl, paths, rootDir, outDir, rootDirs, ... 都没有成功。
typescript ×9
monorepo ×3
tsc ×3
tsconfig ×2
build ×1
git ×1
gulp ×1
lerna ×1
node-config ×1
node.js ×1
npm ×1
transpiler ×1
ts-loader ×1
webpack ×1
yarnpkg ×1