小编TS *_*S B的帖子

在引用的项目中使用别名路径

从 TypeScript 3 开始,可以使用项目作为给定项目中的引用。

我试图在我的两个个人项目中使用这样的功能,但它不起作用。所以我决定使用给定的示例作为起点:https ://github.com/appzuka/project-references-example

当我启动时npm run build,所有项目都编译成功。然后我决定修改tsconfig.jsonAnimals文件以包含别名的支持。该文件被修改如下:

{
  "extends": "../../tsconfig-base.json",
  "compilerOptions": {
    "outDir": "lib",
    "rootDir": ".",
    "baseUrl": ".",
    "paths": { "@animals/*": [ "*" ] }
  },
  "references": [
    { "path": "../core" }
  ]
}
Run Code Online (Sandbox Code Playgroud)

(在compilerOptions中添加了baseUrlpaths属性)

然后我这样修改了dog.tsx文件的第一次导入:
import { Animal, Size } from @animals/animal;

当我尝试再次构建主项目时,它失败了。错误是:

ERROR in C:\Users\User\Documents\project-references-example\packages\animals\dog.tsx
[tsl] ERROR in C:\Users\User\Documents\project-references-example\packages\animals\dog.tsx(1,30)
      TS2307: Cannot find module '@animals/animal' or its corresponding type declarations.
Run Code Online (Sandbox Code Playgroud)

为了使其正常工作,我还需要在主项目上添加别名,如下所示(在tsconfig.json …

alias compilation reference node.js typescript

8
推荐指数
0
解决办法
632
查看次数

标签 统计

alias ×1

compilation ×1

node.js ×1

reference ×1

typescript ×1