You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
seems like P.instanceOf doesn't resolve prototype chain when inheritance doesn't add anything
class GrandParentClass {
private doesNotHelpToAddSomething = "soTsPatternCanDifferBetweenInstances";
constructor(public message: string) {
this.message = message;
}
}
class UncleClass extends GrandParentClass {
// private helpsToAddSomething = "soTsPatternCanDifferBetweenInstances";
}
class ParentClass extends GrandParentClass {
// private helpsToAddSomething = "soTsPatternCanDifferBetweenInstances";
}
class ChildClass extends ParentClass {}
const instance: GrandParentClass = new ParentClass("!");
// it resovles to never after ChildClass .with unless we add 'helpsToAddSomething' to ParentClass
// if adding 'helpsToAddSomething' only to Parent than Uncle is resolved but Parent is still never
const res = match(instance)
.with(P.instanceOf(ChildClass), (e) => `this is Child${e.message}`)
.with(
P.instanceOf(UncleClass),
(e) => /*e is never*/ `this is Uncle${e.message}`
)
.with(
P.instanceOf(ParentClass),
(e) => /*e is never*/ `this is Parent${e.message}`
)
.run();
// wiil output: 'this is Parent!'
console.log(res);
js instaceof does look at the prototype correctly
// will output 'instance is instanceof ParentClass'
if (instance instanceof ChildClass) {
console.log("instance is instanceof ChildClass");
} else {
if (instance instanceof UncleClass) {
console.log("instance is instanceof UncleClass");
}
if (instance instanceof ParentClass) {
console.log("instance is instanceof ParentClass");
} else {
if (instance instanceof GrandParentClass) {
console.log("instance is instanceof GrandParentClass");
}
}
}
Describe the bug
seems like P.instanceOf doesn't resolve prototype chain when inheritance doesn't add anything
js instaceof does look at the prototype correctly
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain
Code Sandbox with a minimal reproduction case
https://codesandbox.io/s/ts-pattern-instanceof-not-resolving-bwmk5d?file=/src/index.ts
Versions
The text was updated successfully, but these errors were encountered: