Typescript conditional types and classes - strange behavior

Viewed 43
class A {
    x = 1
}

class B {
    x = 1
}

type ThisIsTrue = B extends A ? true : false

Guess the above returns true since B is assignable to A as both have { x: number }.

But when the same field is set to private, the types are no longer assignable!

class C {
    private x = 1
}

class D {
    private x = 1
}

type ThisIsFalse = D extends C ? true : false

Is there a reason why the above returns false?

But when a class extends C, then it is again treated as assignable...

class E extends C {
}

type ThisIsAgainTrue = E extends C ? true : false

Does this mean just by adding a private field to a class we can actually enable "instance type" checks for classes?!

type InstanceOf<D, B> = D extends B ? true : false // provided B has a private field
1 Answers
Related