我知道我遺漏了一些明顯的東西。下面是實際在生產中作業的偽代碼。但是,IDE(IntelliJ IDEA)給了我一個我無法破解的錯誤。可以用 來抑制它noinspection TypeScriptValidateTypes,但是,我真正想知道的是發生了什么:
this.authService.onAuthStateChanged()
.pipe(
withLatestFrom(
this.activatedRoute.queryParams.pipe(
map(params => params['returnUrl'])
)
)
)
.subscribe(([authState, returnUrl]) => {
// [...]
});
我上subscribe線的錯誤:
引數型別 ([authState, returnUrl]: readonly [any, any]) => void 不可分配給引數型別 Partial<Observer<[(AuthState | undefined), ...unknown[]]>> | 不明確的
其他相關細節?
// AuthState is just a POJO.
onAuthStateChanged(): BehaviorSubject<AuthState | undefined>
// This is Angular's params object.
// https://angular.io/api/router/Params
export declare type Params = {
[key: string]: any;
};
我正在使用RxJS ~7.5.0, 和Typescript ~4.7.4. 我也在使用Angular ^14.2.0,盡管這確實不是 Angular 問題。
那么,這里應該發生什么?onAuthStateChanged發出AuthState或undefined取決于用戶是否登錄。這與一個名為 的查詢引數結合在一起,當然returnUrl,它是 a string。
現在,過去使用的語法的作業方式是使用解構賦值簽名可以使用兩個可觀察物件([authState, returnUrl])——至少我認為這就是所謂的。正如我所說,它可以在現實生活中正確編譯和執行而且正如其他人在這里展示的那樣, StackBlitz上也不會出錯(更新以更接近地匹配我的環境與相同tslint等angular.json)。
然而,我心愛的 IntelliJ...
是不是我在看最新版本的 RxJS 中的一個重大變化:
與最新從
- 通用簽名已更改。不要顯式傳遞泛型。
來源:https ://rxjs.dev/6-to-7-change-summary#withlatestfrom
這是偉大的,除了它仍然編譯和運行。所以,有兩件事:
- 幕后發生了什么導致 IntelliJ 出現此錯誤?
- 我怎樣才能讓它消失?我試過強烈輸入所有引數,但無濟于事......
uj5u.com熱心網友回復:
該問題在WEB-57220被跟蹤,請關注它以獲取更新
在設定中啟用 Typescript 語言服務| 語言和框架 | TypeScript應該有助于消除錯誤
uj5u.com熱心網友回復:
如果您定義onAuthStateChanged為回傳 a 的函式BehaviourSubject或直接定義為 a BehaviourSubject,則不應出現任何錯誤。
以下代碼不會產生任何型別錯誤
type AuthState = {};
let onAuthStateChanged = new BehaviorSubject<AuthState | undefined>(null);
let onAuthStateChangedFunc: () => BehaviorSubject<AuthState | undefined>;
// This is Angular's params object.
// https://angular.io/api/router/Params
type Params = {
[key: string]: any;
};
class Foo {
constructor(private activatedRoute: ActivatedRoute) {}
foo() {
onAuthStateChangedFunc() // or
// onAuthStateChanged
.pipe(
withLatestFrom(
this.activatedRoute.queryParams.pipe(
map((params) => params['returnUrl'])
)
)
)
.subscribe(([authState, returnUrl]) => {
// [...]
});
}
}
變數[authState, returnUrl]被正確識別型別AuthState和any分別。
請參閱此 stackblitz以供參考
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/505771.html
