Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

P.instanceOf() not work with abstract classes #120

Closed
MiniSuperDev opened this issue Nov 3, 2022 · 1 comment
Closed

P.instanceOf() not work with abstract classes #120

MiniSuperDev opened this issue Nov 3, 2022 · 1 comment

Comments

@MiniSuperDev
Copy link

Describe the bug
When use P.instanceOf() for an abstract class type, it show the follow errors:

Argument of type 'typeof A' is not assignable to parameter of type 'AnyConstructor'.
Cannot assign an abstract constructor type to a non-abstract constructor type.

Code Sandbox with a minimal reproduction case

import {match, P} from 'ts-pattern';

abstract class A {
  abstract aFun(): string;
}
abstract class B {
  abstract bFun(): string;
}

class AImpl extends A {
  aFun(): string {
    return 'a';
  }
}

const object: A | B = new AImpl();

console.log(
  match(object as A | B)
    .with(P.instanceOf(A), a => a.aFun())
    .with(P.instanceOf(B), b => b.bFun())
    .exhaustive()
);

It works normally it the classes are not abstract:

Code:
import {match, P} from 'ts-pattern';

class A {
  aFun(): string {
    return 'a';
  }
}
class B {
  bFun(): string {
    return 'b';
  }
}

class AImpl extends A {
  aFun(): string {
    return 'a';
  }
}

const object: A | B = new AImpl();

console.log(
  match(object as A | B)
    .with(P.instanceOf(A), a => a.aFun())
    .with(P.instanceOf(B), b => b.bFun())
    .exhaustive()
);

Versions

  • TypeScript version: 4.7.0
  • ts-pattern version: 4.0.5
  • environment: Node v16.13.1
@gvergnaud
Copy link
Owner

Thanks for reporting, I just fixed that in version 4.0.6!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants