我认为以下情况应该失败:
class A { a: boolean; }
class B extends A { b: boolean; }
// this works, thus "proving" B is a subtype of A
const a: A = new B();
// however, this also works! It should fail.
class C { c: boolean; } // a C cannot be assigned to A, it's not a subtype
type SubtypeAcceptor<T> = ($Subtype<T>) => boolean;
const a2: SubtypeAcceptor<A> = (c: C) => (typeof c === 'object');
是什么给出的?上述的非通用版本也会失败:
type SubtypeAcceptorOfA = ($Subtype<A>) => boolean;
const a3: SubtypeAcceptorOfA = (c: C) => (typeof c === 'object');
这是因为理论上可以C & $Subtype<A>
吗?上面的确切对象类型也失败如下:
const a4: SubtypeAcceptor<{| a: boolean |}> = (c: {| b: boolean |}) => c.b;
我认为{| b: boolean |}
显然不能是{| a: boolean |}
的子类型。