【问题标题】:Unable to find type definitions when using typescript 3.0.1 project refferences使用 typescript 3.0.1 项目引用时找不到类型定义
【发布时间】:2019-01-18 19:38:48
【问题描述】:

我最初有以下项目结构(monorepo)

_app/
_firebase/
_types/
tsconfig.json

这里,根级别的 tsconfig 用于 _app_firebase 打字稿项目。 _types 是一个包含models.tsstore.ts 等文件的文件夹……这些文件只是定义了不同的接口。

我能够使用这些接口,而无需在 _app_firebase 项目中导入/导出任何东西之前没有任何问题。

我进行了更改以使用 typescripts v3 项目引用,所以我的应用程序结构现在看起来像这样

_app/
  tsconfig.json
_firebase/
  tsconfig.json
_types/
tsconfig.json

根级别tsconfig.json 有以下内容

{
  "compilerOptions": {
    "composite": true
  },
  "references": [
    { 
      "path": "_app" 
    }
  ]
}

其他配置只有简单的选项,例如outDirtargetlib 等...

现在,如果我在_app_firebase 的任何位置使用_types 文件夹中的接口,我会收到类似的错误

[ts] 找不到名称“IInventoryModel”。

有什么方法可以保留以前的功能,无需显式导出/导入即可使用这些接口,同时仍能利用新项目的引用功能?

编辑:这里是 _app 的 tsconfig 示例

{
  "compilerOptions": {
    "target": "es2016",
    "module": "commonjs",
    "lib": ["es2016"],
    "jsx": "react",
    "outDir": "./dist",
    "strict": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
  }
}
_types 文件夹内的

EDIT 2 接口声明如下,即 _types/models.ts 具有

interface ICharacterModel { /* ... */ }

interface IInventoryModel { /* ... */ }

// etc...

【问题讨论】:

    标签: reactjs typescript tsconfig


    【解决方案1】:

    对于初学者,_types 需要自己的tsconfig.json_app 需要引用它。然后,基于the documentation,看起来神奇的步骤是在_types 上设置outFile。以下配置对我有用:

    // _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"
        }
    }
    

    【讨论】:

    • 似乎无法做到这一点,您能否澄清/提供回购示例,说明这对您有用吗?我在_types 文件夹中创建了 tsconfig.json,添加了outFile 并从_app 引用它,但_app 中使用的接口在_types 中定义仍然会抛出该错误。我是否也保留了位于项目根目录中的 tsconfig.json 文件?
    • 这里:github.com/mattmccutchen/stackoverflow-51799200。在_app 目录中运行tsc -b .。是否将tsconfig.json 保留在根目录中无关紧要。
    • 完全忘记了--build,它现在可以工作了:) 我可以看到一些错误,但是tsc -b --skipLibCheck 导致skipLibCheck 不是文件错误,所以我想我会给更多时间项目参考成熟。 VSCode 目前似乎也不支持它
    • 试试tsc --skipLibCheck -b .。 VS Code 对我来说工作正常;确保 TypeScript 版本在状态栏中显示为 3.0.1。
    • 确实是 3.0.1,我检查了你的 repo IInventoryModel 错误和 [ts] Cannot find name 'IInventoryModel'. 为我:/ 这里有什么需要为 vs 代码启用的特定设置吗?跨度>
    猜你喜欢
    • 2021-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-10
    • 2017-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多