Open LumaKernel opened 1 month ago
here, it's important to combine
By trying {} instanceof {}
, I got the following message.
The right-hand side of an 'instanceof' expression must be either of type
'any', a class, function, or other type assignable to the 'Function'
interface type, or an object type with a 'Symbol.hasInstance' method.
So it seems we can just use Function | { readonly [Symbol.hasInstance]: any }
instead to support any types which can be also used in instanceof
right-hand operand.
Ah thanks for the report, that's too bad. I'm not sure I can fix it in ts-pattern
because instanceOf requires it's parameter to be a subtype of abstract new (...any) => any
in order to pass it to the native InstanceType
helper. Without this contraint, I can't narrow the input type for this branch:
export class Color {
private constructor(
public readonly r: number,
public readonly g: number,
public readonly b: number,
) {}
static new(r: number, g: number, b: number): Color {
return new Color(r, g, b);
}
}
type X = InstanceType<Color> // ❌
Describe the bug A clear and concise description of what the bug is.
if (instanceof C)
can be used for classes which has private constructors potentially, butP.instanceof
can't. It's becausetype AnyConstructor = abstract new (...args: any[]) => any;
cannot accept private constructors.TypeScript playground with a minimal reproduction case
Playground
Versions
I tried to find the universal class of all classes including private constructors, but I coundn't get it. Maybe I'm hitting the limitation of TypeScript...