Skip to content

is do not accept classes with private/protected constructors #143

Description

@GerkinDev

Given the following code:

class Foo {
  #type = "foo";
}
class Bar {
  #type = "bar";
  protected constructor() {}
}
abstract class Baz {
  #type = "baz";
  protected constructor() {}
}

const list: Array<Foo | Bar | Baz> = [];
// OK
const foos: Foo[] = list.filter(is(Foo));
// Argument of type 'typeof Bar' is not assignable to parameter of type 'abstract new (...args: any[]) => any'.
//  Cannot assign a 'protected' constructor type to a 'public' constructor type.
const bars: Bar[] = list.filter(is(Bar));
const bazs: Baz[] = list.filter(is(Baz));

However, I managed to get it work in the 3 samples above using the following declaration:

type PrivateConstructor<TClass = any> = { new (): never } & TClass;
declare function is<C extends PrivateConstructor<any>>(ctor: C): (val: any) => val is InstanceType<PrivateConstructor<C>>;

Based on https://stackoverflow.com/a/78887842

I did not checked for more situations with the suggested declaration

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions