解决 Angular 官网下载的库 Schematics 执行 npm run build 时遇到的编译错误
我在 Angular 官網(wǎng)下載的 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’.
解決方法
這個 project 的數(shù)據(jù)類型是我們自己在 schema.ts 里定義的,故調(diào)整成和 workspace.extensions.defaultProject 一致即可。
修改之前:
修改之后,又遇到了新問題:Cannot find name ‘JsonArray’
其實直接在原始代碼位置加上 as string 即可,意思是告訴編譯器,程序員非常清楚,在這個上下文里,workspace.extensions.defaultProject 的類型,一定是 string.
修復之后遇到另一個錯誤:
‘options’ is declared but its value is never read.ts(6133)
這個錯誤的解決方案比較簡單:
參數(shù)名前加上一個下劃線即可。
最后,npm run build 成功執(zhí)行:
更多Jerry的原創(chuàng)文章,盡在:“汪子熙”:
總結(jié)
以上是生活随笔為你收集整理的解决 Angular 官网下载的库 Schematics 执行 npm run build 时遇到的编译错误的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 解决 Angular 官网下载的库 Sc
- 下一篇: 微博开放平台api使用[通俗易懂](微博