关于angular:解决-Angular-官网下载的库-Schematics-执行-npm-run-build-时遇到的编译错误

0次阅读

共计 1597 个字符,预计需要花费 4 分钟才能阅读完成。

我在 Angular 官网下载的 library Schematics 例子,运行命令行 npm run build 时,遇到如下谬误:

npm run build

my-lib@0.0.1 build c:\Code\SPA\schematics-for-libraries\projects\my-lib
tsc -p tsconfig.schematics.json

schematics/my-service/index.ts:39:7 – error TS2322: Type ‘string | number | boolean | JsonArray | JsonObject | null | undefined’ is not assignable to type ‘string | undefined’.
Type ‘null’ is not assignable to type ‘string | undefined’.

39 options.project = workspace.extensions.defaultProject;


schematics/my-service/index.ts:42:44 - error TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
  Type 'undefined' is not assignable to type 'string'.
![](https://img-blog.csdnimg.cn/img_convert/f2e94d1451031e232d6f55e81ce12e91.png)

![](https://img-blog.csdnimg.cn/img_convert/8ce2f806cf11004fc7b34d1da6022012.png)

# 解决办法

这个 project 的数据类型是咱们本人在 schema.ts 里定义的,故调整成和 workspace.extensions.defaultProject 统一即可。批改之前:![](https://img-blog.csdnimg.cn/img_convert/fa25d1a0ef69801022fa2568faa46f96.png)

批改之后,又遇到了新问题:Cannot find name 'JsonArray'

![](https://img-blog.csdnimg.cn/img_convert/6a070732664af2f6255f8fc25f738c58.png)

其实间接在原始代码地位加上 as string 即可,意思是通知编译器,程序员十分分明,在这个上下文里,workspace.extensions.defaultProject 的类型,` 肯定 ` 是 string.

![](https://img-blog.csdnimg.cn/img_convert/9361a16756a57a9b969ec52665b77b7f.png)

修复之后遇到另一个谬误:> 'options' is declared but its value is never read.ts(6133)

![](https://img-blog.csdnimg.cn/img_convert/f7efb0f461417469bb9f76eb0a121bad.png)

这个谬误的解决方案比较简单:参数名前加上一个下划线即可。![](https://img-blog.csdnimg.cn/img_convert/adc984e2bfe260baacc8793cd902ef90.png)

最初,npm run build 胜利执行:![](https://img-blog.csdnimg.cn/img_convert/2ef3bb413e40355fe9cb71b7eceba3be.png)


更多 Jerry 的原创文章,尽在:"汪子熙":
正文完
 0