TS2674

Constructor of class 'Node' is protected and only accessible within the class declaration.

Broken Code ❌

class Node<NodeType> {
  protected constructor(public value: NodeType) {}
}
 
const node = new Node<number>(5);

This error occurs because the Node class's constructor is marked as protected, so it cannot be instantiated directly outside the class.

Fixed Code ✔️

To fix this, either change the constructor to public if you want instances to be created directly:

class Node<NodeType> {
  public constructor(public value: NodeType) {}
}
 
const node = new Node<number>(5);

Alternatively, you can add a static factory method within the class to control instance creation while keeping the constructor protected:

class Node<NodeType> {
  protected constructor(public value: NodeType) {}
 
  static create<NodeType>(value: NodeType): Node<NodeType> {
    return new Node(value);
  }
}
 
const node = Node.create<number>(5);