1

So I have this scenario where I perform an exclude and I am left with my expected type. When I try to instantiate a class with my type I get this error from the typescript compiler Type 'MyClass' has no construct signatures

class MyClass {
}

type U = Exclude<MyClass | string, string> // U = MyClass

// ...

function example(_myClass: U) {
    const instance = new _myClass();
    // ts error `Type 'MyClass' has no construct signatures`
}

Playground link

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
ioedeveloper
  • 528
  • 3
  • 11
  • 1
    Write `type U = Exclude` and it works. – ford04 Feb 05 '21 at 15:04
  • 1
    @ford04 make your comment an answer so I can select it. – ioedeveloper Feb 05 '21 at 15:07
  • @ford04 - What ioedeveloper said. But also include an explanation of what's going on (if you know). :-) – T.J. Crowder Feb 05 '21 at 15:08
  • @ioedeveloper `typeof MyClass` is the static type of `MyClass` constructor function (the one which enables you to create instances from it), whereas `MyClass` would be the already instantiated type (thats why you get that error with `new`). I guess, [this](https://stackoverflow.com/questions/39614311/class-constructor-type-in-typescript) and [this](https://stackoverflow.com/questions/39392853/is-there-a-type-for-class-in-typescript-and-does-any-include-it) post already give quite good explanations. Please elaborate a bit more, if things are unclear. – ford04 Feb 05 '21 at 15:42

0 Answers0