尝试实现通用规范和访问者模式时,类型不满足约束并错误地扩展了接口

Type does not satisfy the constraint and incorrectly extends interface when trying to implement generic Specification and Visitor patterns

我正在尝试同时实现通用规范模式和通用访问者模式。这是我的基本接口。

export interface Specification<T, TVisitor extends SpecificationVisitor<TVisitor, T>> {
  accept(visitor: TVisitor): void;
  isSatisfiedBy(candidate: T): boolean;
  and(other: Specification<T, TVisitor>): Specification<T, TVisitor>;
  andNot(other: Specification<T, TVisitor>): Specification<T, TVisitor>;
  or(other: Specification<T, TVisitor>): Specification<T, TVisitor>;
  orNot(other: Specification<T, TVisitor>): Specification<T, TVisitor>;
  not(): Specification<T, TVisitor>;
}

export interface SpecificationVisitor<TVisitor extends SpecificationVisitor<TVisitor, T>, T> {
  visit(specification: AndSpecification<T, TVisitor>): void;
  visit(specification: AndNotSpecification<T, TVisitor>): void;
  visit(specification: OrSpecification<T, TVisitor>): void;
  visit(specification: OrNotSpecification<T, TVisitor>): void;
  visit(specification: NotSpecification<T, TVisitor>): void;
}

为方便起见,我为基本布尔运算符实现了一些基础 classes 和抽象 class。

export abstract class CompositeSpecification<T, TVisitor extends SpecificationVisitor<TVisitor, T>> implements Specification<T, TVisitor> {
  abstract isSatisfiedBy(candidate: T): boolean;
  abstract accept(visitor: TVisitor): void;

  and(other: Specification<T, TVisitor>): Specification<T, TVisitor> {
    return new AndSpecification<T, TVisitor>(this, other);
  }
  andNot(other: Specification<T, TVisitor>): Specification<T, TVisitor> {
    return new AndNotSpecification<T, TVisitor>(this, other);
  }
  or(other: Specification<T, TVisitor>): Specification<T, TVisitor> {
    return new OrSpecification<T, TVisitor>(this, other);
  }
  orNot(other: Specification<T, TVisitor>): Specification<T, TVisitor> {
    return new OrNotSpecification<T, TVisitor>(this, other);
  }
  not(): Specification<T, TVisitor> {
    return new NotSpecification<T, TVisitor>(this);
  }
}

export class AndSpecification<T, TVisitor extends SpecificationVisitor<TVisitor, T>> extends CompositeSpecification<
  T,
  TVisitor
> {
  constructor(readonly left: Specification<T, TVisitor>, readonly right: Specification<T, TVisitor>) {
    super();
  }

  accept(visitor: TVisitor): void {
    visitor.visit(this);
  }

  isSatisfiedBy(candidate: T): boolean {
    return this.left.isSatisfiedBy(candidate) && this.right.isSatisfiedBy(candidate);
  }
}

export class AndNotSpecification<T, TVisitor extends SpecificationVisitor<TVisitor, T>> extends CompositeSpecification<T, TVisitor> {
  constructor(readonly left: Specification<T, TVisitor>, readonly right: Specification<T, TVisitor>) {
    super();
  }

  accept(visitor: TVisitor): void {
    visitor.visit(this);
  }

  isSatisfiedBy(candidate: T): boolean {
    return this.left.isSatisfiedBy(candidate) && !this.right.isSatisfiedBy(candidate);
  }
}

export class OrSpecification<T, TVisitor extends SpecificationVisitor<TVisitor, T>> extends CompositeSpecification<
  T,
  TVisitor
> {
  constructor(readonly left: Specification<T, TVisitor>, readonly right: Specification<T, TVisitor>) {
    super();
  }

  accept(visitor: TVisitor): void {
    visitor.visit(this);
  }

  isSatisfiedBy(candidate: T): boolean {
    return this.left.isSatisfiedBy(candidate) || this.right.isSatisfiedBy(candidate);
  }
}

export class OrNotSpecification<T, TVisitor extends SpecificationVisitor<TVisitor, T>> extends CompositeSpecification<
  T,
  TVisitor
> {
  constructor(readonly left: Specification<T, TVisitor>, readonly right: Specification<T, TVisitor>) {
    super();
  }

  accept(visitor: TVisitor): void {
    visitor.visit(this);
  }

  isSatisfiedBy(candidate: T): boolean {
    return this.left.isSatisfiedBy(candidate) || !this.right.isSatisfiedBy(candidate);
  }
}

export class NotSpecification<T, TVisitor extends SpecificationVisitor<TVisitor, T>> extends CompositeSpecification<
  T,
  TVisitor
> {
  constructor(readonly other: Specification<T, TVisitor>) {
    super();
  }

  accept(visitor: TVisitor): void {
    visitor.visit(this);
  }

  isSatisfiedBy(candidate: T): boolean {
    return !this.other.isSatisfiedBy(candidate);
  }
}

以上所有工作和编译都没有错误。但是,当我 运行 尝试创建一个扩展基本 SpecificationVisitor 接口的接口并实现一个扩展抽象 CompositeSpecification 的 class 时,编译器出现问题。

export interface NumberComparatorVisitor extends SpecificationVisitor<NumberComparatorVisitor, number> {
  visit(specification: GreaterThan): void;
}

export class GreaterThan extends CompositeSpecification<number, NumberComparatorVisitor> {
  constructor(readonly value: number) {
    super();
  }

  accept(visitor: NumberComparatorVisitor): void {
    visitor.visit(this);
  }

  isSatisfiedBy(candidate: number): boolean {
    return candidate > this.value;
  }
}

我收到以下错误:

Type 'NumberComparatorVisitor' does not satisfy the constraint 'SpecificationVisitor<NumberComparatorVisitor, number>'.ts(2344)

Interface 'NumberComparatorVisitor' incorrectly extends interface 'SpecificationVisitor<NumberComparatorVisitor, number>'.
  Types of property 'visit' are incompatible.
    Type '(specification: GreaterThan) => void' is not assignable to type '{ (specification: AndSpecification<number, NumberComparatorVisitor>): void; (specification: AndNotSpecification<number, NumberComparatorVisitor>): void; (specification: OrSpecification<...>): void; (specification: OrNotSpecification<...>): void; (specification: NotSpecification<...>): void; }'.
      Types of parameters 'specification' and 'specification' are incompatible.
        Type 'AndSpecification<number, NumberComparatorVisitor>' is not assignable to type 'GreaterThan'.ts(2430)

Type 'NumberComparatorVisitor' does not satisfy the constraint 'SpecificationVisitor<NumberComparatorVisitor, number>'.
  Types of property 'visit' are incompatible.
    Type '(specification: GreaterThan) => void' is not assignable to type '{ (specification: AndSpecification<number, NumberComparatorVisitor>): void; (specification: AndNotSpecification<number, NumberComparatorVisitor>): void; (specification: OrSpecification<...>): void; (specification: OrNotSpecification<...>): void; (specification: NotSpecification<...>): void; }'.
      Types of parameters 'specification' and 'specification' are incompatible.
        Property 'value' is missing in type 'AndSpecification<number, NumberComparatorVisitor>' but required in type 'GreaterThan'.ts(2344)

我不太明白它为什么抱怨。我需要更改什么才能使它按我预期的方式工作?

哇,里面有很多代码。让我们将其缩减为显示相同问题的 minimal reproducible example

interface Foo {
  ovld(x: string): number;
  ovld(x: number): boolean;
}

interface BadBar extends Foo {  // error!
  ovld(x: boolean): string;
}

这里有一个 Foo 接口和一个名为 ovldoverloaded 方法。此方法有两个调用签名;一个需要 string,另一个需要 number。我们尝试创建扩展它的 BadBar 接口。我们的目的是向 ovld 添加第三个重载,它接受 boolean。但它不起作用!为什么?


答案是您不能在扩展接口时简单地添加重载。接口扩展是不是interface merging。通过在扩展接口中重新声明 ovld,您告诉编译器用 BadBar 中的版本完全覆盖 Fooovld 的类型。并且因为 BadBar 中的单个调用签名不能分配给 Foo 中的调用签名,这是一个错误,就像这是一个错误:

interface XXX {
  prop: string;
}
interface YYY extends XXX { // error!
  prop: boolean; 
}

在这两种情况下,您都在通过采用现有的 属性 或方法并进行无效更改来错误地扩展接口。唯一可接受的更改是 缩小 更改,例如将 string 更改为 "a" | "b"


那么,如果我们不能仅通过在 BadBar 中重新声明 ovld() 来添加重载,我们如何 做到这一点?一种方法是将 indexed access type to get the existing call signatures from the parent interface, and then intersect 与新的调用签名一起使用:

interface GoodBar extends Foo {
  ovld: ((x: boolean) => string) & Foo["ovld"]
} 
/* (property) GoodBar.ovld: ((x: boolean) => string) & {
  (x: string): number;
  (x: number): boolean;
} */

交集 X & Y 将被视为 X 的有效缩小,因此编译器错误消失。此外,TypeScript 认为函数的交集等同于重载。因此,上面为您提供了新的 (x: boolean) => string 调用签名作为第一个重载,然后是来自 Foo:

的现有两个重载签名
declare const goodBar: GoodBar;
goodBar.ovld(false).toUpperCase();
goodBar.ovld("hello").toFixed();
goodBar.ovld(123) === true;

万岁!


这意味着您的 NumberComparatorVisitor 可能需要更改为如下内容:

export interface NumberComparatorVisitor extends
  SpecificationVisitor<NumberComparatorVisitor, number> {
  visit: (
    ((specification: GreaterThan) => void) &
    SpecificationVisitor<NumberComparatorVisitor, number>["visit"]
  );
}

如果我进行更改,错误就会消失,因为现在 NumberComparatorVisitor 确实是其父接口的有效扩展。

Playground link to code