combineLatest 和严格类型错误 - "No overload matches this call."

Error with combineLatest and strict typings - "No overload matches this call."

最近将项目从 Angular v9 更新到 v11。选择了严格的打字,当然得到了无数的错误,难倒了这个。

使用 combineLatest 从 3 个 observables 生成一个发布(每个都是一个 firebase doc.valueChanges() 或 collection.valueChanges())

我的代码:

getEdition(editionId: string): AngularFirestoreDocument<Edition> {
    return this.afs.collection('editions').doc(editionId);
}

const editionRef: Observable<Edition | undefined> = this.editionService.getEdition(ed.id).valueChanges();

combineLatest([editionRef, sectionsRef, articlesRef]).subscribe(([edition, sections, articles]: [Edition, Section[], Article[]]) => {
   // do stuff, returns void
});

Edition.ts:

import { Moment } from 'moment';

export interface Edition {
  id: string;
  pubTypeId: string;
  date: string | Moment;
  imgCaption: string;
  imgCredit: string;
  imgLink: string;
  imgSrc: string;
  introText: string;
  hasPreface: boolean;
  preface: string;
  printLink: string;
}

我的错误:

error TS2769: No overload matches this call.
  Overload 1 of 5, '(observer?: NextObserver<[Edition | undefined, DocumentData, DocumentData]> | ErrorObserver<[Edition | undefined, DocumentData, DocumentData]> | CompletionObserver<...> | undefined): Subscription', gave the following error.
    Argument of type '([edition, sections, articles]: [Edition, Section[], Article[]]) => void' is not assignable to parameter of type 'NextObserver<[Edition | undefined, DocumentData, DocumentData]> | ErrorObserver<[Edition | undefined, DocumentData, D
ocumentData]> | CompletionObserver<...> | undefined'.
      Property 'complete' is missing in type '([edition, sections, articles]: [Edition, Section[], Article[]]) => void' but required in type 'CompletionObserver<[Edition | undefined, DocumentData, DocumentData]>'.
  Overload 2 of 5, '(next?: ((value: [Edition | undefined, DocumentData, DocumentData]) => void) | undefined, error?: ((error: any) => void) | undefined, complete?: (() => void) | undefined): Subscription', gave the following error.
    Argument of type '([edition, sections, articles]: [Edition, Section[], Article[]]) => void' is not assignable to parameter of type '(value: [Edition | undefined, DocumentData, DocumentData]) => void'.
      Types of parameters '__0' and 'value' are incompatible.
        Type '[Edition | undefined, DocumentData, DocumentData]' is not assignable to type '[Edition, Section[], Article[]]'.
          Type 'Edition | undefined' is not assignable to type 'Edition'.
            Type 'undefined' is not assignable to type 'Edition'.

我知道我需要以某种方式调整我的打字,但鉴于我在这里使用的是 combineLatest,我不确定该怎么做...

我的猜测是 editionRef -> edition -> Edition 链中的某处有一个隐含的 any 类型。来自文档:

Any variable, parameter or property that is initialized with null or undefined will have type any, even if strict null checks is turned on.

所以我会首先尝试在该链中找到一个未初始化的 undefinednull 变量。

错误

Type 'undefined' is not assignable to type 'Edition'.

表示 this.editionService.getEdition(ed.id).valueChanges() 永远不允许 return undefined 并且 Observable 的类型只能是 Edition.

最后,在您的修复中,最好实际指明需要强制执行的类型。

可以 我们 as 对类型进行类型转换,但您正在将您不确定的内容更改为您需要的内容。此选项可能会起作用,但不会按照输入的方式“强制”输入。

所以最好的解决方案,正如您在评论中所说:

subscribe(([edition, sections, articles]: [Edition | undefined, Section[], Article[]]) => { ... }