Quick Answer
Calling new on non-constructable values
Understanding the Issue
TypeScript prevents using new with values that aren't constructors. This error occurs when the target lacks a construct signature.
The Problem
This code demonstrates the issue:
Typescript
Error
// Problem: Invalid constructor
interface Foo { bar: string; }
const foo: Foo = { bar: "test" };
new foo(); // Error
The Solution
Here's the corrected code:
Typescript
Fixed
// Solution 1: Use actual constructor
class Bar {
constructor() { ... }
}
new Bar();
// Solution 2: Check for construct signature
interface Constructable {
new (): any;
}
function createInstance(c: Constructable) {
return new c();
}
Key Takeaways
Only use new with values that are known to be constructors.