小编Paw*_*ski的帖子

NRWL NX 库打字稿环境声明文件

我的新 NX 工作区中有一个基本设置

  • /apps/my-app(节点类型)
  • /libs/some-lib(节点类型)

两者都是使用 nx cli 命令创建的:即nx g @nrwl/node:lib some-lib --simpleModuleName=true

对于库,我想使用环境声明文件(.d.ts)。我在/libs/some-lib/src/types/index.d.ts中添加了一个,内容为:

declare type MyCustomType = any;
Run Code Online (Sandbox Code Playgroud)

据我了解,该文件是根据/libs/some-lib/tsconfig.lib.json文件进行处理的,但我还在/libs/some-lib/tsconfig.json中添加了对此文件的直接引用:

{
    "extends": "../../../tsconfig.base.json",
    "files": [
        "src/types/index.d.ts"
    ],
    "include": [
        "src/types/**/*.d.ts"
    ],
    "references": [
        {
            "path": "./tsconfig.lib.json"
        },
        {
            "path": "./tsconfig.spec.json"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

当我在 lib 代码中使用此文件时,我的 IDE 很高兴,例如: /libs/some-lib/src/lib/file.ts

export const x: MyCustomType = 1;
Run Code Online (Sandbox Code Playgroud)

但是如果我想将此变量导入到my-app中并运行它(nxserve my-app),

import { x } from '@my-workspace/some-lib';
Run Code Online (Sandbox Code Playgroud)

我从编译器得到一个错误:

ERROR in libs/some-lib/src/lib/file.ts
TS2304: Cannot find …
Run Code Online (Sandbox Code Playgroud)

types ambient typescript nrwl

11
推荐指数
1
解决办法
1583
查看次数

标签 统计

ambient ×1

nrwl ×1

types ×1

typescript ×1