我正在嘗試使用異步電子郵件驗證器,此代碼來自教程,但我認為版本已更改并且它會抱怨,老實說我不明白這個錯誤。
validateEmailNotTaken(): AsyncValidatorFn {
return control => {
return timer(500).pipe(
switchMap(() => {
if (!control.value) {
return of(null);
}
return this.accountService.checkEmailExists(control.value).pipe(
map(res => {
return res ? { emailExists: true } : null;
})
);
})
);
};
錯誤:
Type '(control: AbstractControl) => Observable<unknown>' is not assignable to type
'AsyncValidatorFn'.
Type 'Observable<unknown>' is not assignable to type 'Promise<ValidationErrors | null> | Observable<ValidationErrors | null>'.
Type 'Observable<unknown>' is not assignable to type 'Observable<ValidationErrors | null>'.
Type 'unknown' is not assignable to type 'ValidationErrors | null'.
首先,有人可以解釋我如何閱讀這個錯誤嗎?
所以我假設所有錯誤都與第一次回傳有關,但是因為我沒有指定型別,switchMap或者最后它拋出了這個錯誤,我不知道它需要什么型別。
謝謝
編輯:所以我發現這個錯誤來自空引數 switchMap 但我不知道在內部為引數提供什么
Edit2:如果我洗掉最后一個回傳,那么我會在 switchMap 塊中得到一個不同的錯誤:
Argument of type '() => Observable<null> | undefined' is not assignable to parameter of type '(value: 0, index: number) => ObservableInput<any>'.
Type 'Observable<null> | undefined' is not assignable to type 'ObservableInput<any>'.
Type 'undefined' is not assignable to type 'ObservableInput<any>'.
Edit3:每條評論
email: new FormControl('', [
Validators.required,
Validators.pattern('^[\\w-\\.] @([\\w-] \\.) [\\w-]{2,4}$'),
], [this.validateEmailNotTaken()]),
uj5u.com熱心網友回復:
嘗試添加更嚴格的型別 - 可能只是型別不夠明確
validateEmailNotTaken(): AsyncValidatorFn {
return (control: AbstractControl): Observable<ValidationErrors | null> => {
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/490243.html
