reactjs - 使用 typescript 3.0.1 项目引用时找不到类型定义
问题描述
我最初有以下项目结构(monorepo)
_app/
_firebase/
_types/
tsconfig.json
在这里,根级别的 tsconfig 用于_app
和typescript_firebase
项目。_types
是一个包含文件的文件夹models.ts
,store.ts
等等......这些文件只是定义了不同的接口。
我能够使用这些接口,而无需在之前的项目中导入/导出任何内容_app
,_firebase
没有任何问题。
我进行了更改以使用 typescripts v3 项目引用,所以我的应用程序结构现在看起来像这样
_app/
tsconfig.json
_firebase/
tsconfig.json
_types/
tsconfig.json
根级别tsconfig.json
有以下内容
{
"compilerOptions": {
"composite": true
},
"references": [
{
"path": "_app"
}
]
}
其他配置只有简单的选项,如,outDir
等...target
lib
现在,如果我_types
在任何地方使用文件夹中的接口,_app
或者_firebase
我收到类似的错误
[ts] 找不到名称“IInventoryModel”。
有什么方法可以保留以前的功能,在没有显式导出/导入的情况下使用这些接口,同时仍然利用新项目的引用功能?
编辑:这是 tsconfig 的示例_app
{
"compilerOptions": {
"target": "es2016",
"module": "commonjs",
"lib": ["es2016"],
"jsx": "react",
"outDir": "./dist",
"strict": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
}
}
EDIT文件夹内的 2 个接口_types
声明如下,即_types/models.ts
有
interface ICharacterModel { /* ... */ }
interface IInventoryModel { /* ... */ }
// etc...
解决方案
对于初学者,_types
需要它自己的tsconfig.json
并且_app
需要引用它。然后,根据文档,看起来神奇的步骤是设置一个outFile
on _types
。以下配置对我有用:
// _app/tsconfig.json
{
"compilerOptions": {
"composite": true,
"target": "es2016",
"module": "commonjs",
"lib": ["es2016"],
"jsx": "react",
"outDir": "./dist",
"strict": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
},
"references": [
{
"path": "../_types"
}
]
}
// _types/tsconfig.json
{
"compilerOptions": {
"composite": true,
"outFile": "types.js"
}
}
推荐阅读
- maven - JAR 文件已损坏
- php - MySQL JOIN 2 个表并分别获取两个表的总和
- r - 如何在三个或更多矩阵上使用 crossprod?
- php - 如何修复:PDOStatement::execute() 期望参数 1 是数组,字符串在 C:\wamp64\www\invoiceTem01-Create.php 的第 64 行
- c++ - 为什么boost::property_tree::json_parser::read_json的第一个参数流不是const?
- javascript - 如何在笔记本电脑上运行粒子设计?
- java - 如何正确地为密码做一个while循环
- reactjs - TypeError:无法获取未定义或空引用的属性“文本”
- javascript - 使用立即需要该状态进行 API 调用的 useState 挂钩时,如何等待 setState 调用完成?
- react-native - 类 FBSDKPackage 中的构造函数 FBSDKPackage 不能应用于给定类型