[英]Angular 9 Ivy build - extension errors
早上好!
我最近將一個 v8 項目更新到了 v9,它似乎工作正常。 但是,我有一些擴展,編譯器會拋出一些關於它們的錯誤,這在我最近升級的另一個項目中沒有發生。
擴展是構造函數和原型的基本類型。 它們設置得很好 - 相同的設置在我擁有它們的多個項目中都有效。至關重要的是,它們在其他升級項目中運行良好,沒有任何錯誤。
但是,要獲得所有的基本使用確認:
將 iface 添加到typings.d.ts
,例如
interface StringConstructor {
isNullOrEmpty(str: string): boolean;
}
interface String {
padStartWithChar(char: string, totalSize: number): string;
}
實現,例如
export {};
String.isNullOrEmpty = function(str: string): boolean {
// ...
};
String.prototype.padStartWithChar = function(this: string, char: string, totalSize: number): string {
// ...
}
並在main.ts
導入
// ...
import './extensions/string-extensions';
// ...
我的路徑沒有改變,它們都指向所有正確位置的所有正確文件,我可以考慮檢查它們( src/typings.d.ts
在我放置的 src 中, tsconfig.app.ts
包括就像以前一樣,等等)。
這會導致構建錯誤; 對於擴展的所有用法,我收到了垃圾郵件,並且構建最終停止:
...
src/extensions/string-extensions.ts:9:8 - error TS2339: Property 'isNullOrEmpty' does not exist on type 'StringConstructor'.
9 String.isNullOrEmpty = function(str: string): boolean {
...
** Angular Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ **
並嘗試瀏覽Cannot GET/
基本消息中的結果。 但是,它會在保存文件時進行通常的重新構建,其中再次顯示所有這些錯誤消息,但這次無論如何都完成了構建並且可以正常工作。
有沒有人有方向可以指出我可能錯過的方向? 在這兩個項目之間,我檢查了angular.json
、 tsconfig
s、 main.ts
等之間的任何重大變化,但到目前為止我是空的。
謝謝。
編輯:
恢復並再次嘗試升級,擴展在更新指南的第一步中斷,只需使用ng update @angular/core@8 @angular/cli@8
運行輕微更新。
從那次更新開始,它就死了。 跳過那一步(無論如何我已經在運行 v8 應用程序)並嘗試直接升級到 9,遺憾的是也將其殺死。
更新:
理查德的回答似乎已經擺脫了日期擴展錯誤消息的所有欄。 它似乎根本不喜歡DateConstructor
接口的存在,而且在嘗試他的方法時,它似乎也不太喜歡moment
用法。
更新編輯:
日期擴展看起來與其他擴展完全一樣,唯一的區別是那里有一個import moment from 'moment'
行import moment from 'moment'
。 刪除這些東西似乎沒有什么區別。 就像Date
s 與String
s 或Array
s 是不同的野獸......
我不得不刪除export {};
從我的實現文件( extensions.ts
)並重復界面。
擴展.ts
interface String {
format(...args: string[]): string;
endsWith(searchString: string, position?: number): boolean;
}
if (!String.prototype.hasOwnProperty('format')) {
String.prototype.format = function (...args: string[]): string {
...
};
}
if (!String.prototype.hasOwnProperty('endsWith')) {
String.prototype.endsWith = function (searchString, position) {
...
};
}
另一個項目有錯誤,我似乎已經縮小了范圍!
我已經構建了類似於Add only entry points to the 'files' or 'include' properties in your tsconfig
。
在我正在更新的這個最新項目中查看並比較了我的tsconfig.app.json
文件與另一個文件,並結合上述警告,它脫穎而出!
以前的配置:
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/app",
"baseUrl": "./",
"module": "es2015",
"types": []
},
"exclude": [
"test.ts",
"**/*.spec.ts"
]
}
新配置,修復警告(添加文件 main 和 polyfills),然后還修復擴展:
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/app",
"types": ["node"]
},
"files": [
"main.ts",
"polyfills.ts"
],
"include": [
"**/*.d.ts",
"extensions/*.ts"
],
"exclude": [
"test.ts",
"**/*.spec.ts"
]
}
似乎需要包含這樣的文件!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.