I don't know how to write root type
The following code:
class BST {
// root is `BST.Node`'s instance
root: ???;
constructor (key: number, value: any) {
this.root = new BST.Node(key, value)
}
static Node = class {
key: number
value: any
constructor (key: number, value: any) {
this.key = key
this.value = value
}
}
}
const t = new BST(1, 'data')
console.log(t.root instanceof BST.Node) // expected: true
You can use InstanceType:
class BST {
// root is `BST.Node`'s instance
root: InstanceType<typeof BST.Node>;
constructor (key: number, value: any) {
this.root = new BST.Node(key, value)
}
static Node = class {
key: number
value: any
constructor (key: number, value: any) {
this.key = key
this.value = value
}
}
}
const t = new BST(1, 'data')
console.log(t.root instanceof BST.Node) // expected: true