TreeNodeSchemaClass TypeAlias
Tree node schema which is implemented using a class.
Signature
/** @sealed */
export type TreeNodeSchemaClass<Name extends string = string, Kind extends NodeKind = NodeKind, TNode extends TreeNode = TreeNode, TInsertable = never, ImplicitlyConstructable extends boolean = boolean, Info = unknown, TConstructorExtra = never> = TreeNodeSchemaCore<Name, Kind, ImplicitlyConstructable, Info, TInsertable> & (undefined extends TConstructorExtra ? {
new (data?: TInsertable | InternalTreeNode | TConstructorExtra): Unhydrated<TNode>;
} : {
new (data: TInsertable | InternalTreeNode | TConstructorExtra): Unhydrated<TNode>;
});
Type Parameters
Parameter | Constraint | Default | Description |
---|---|---|---|
Name | string | string | |
Kind | NodeKind | NodeKind | |
TNode | TreeNode | TreeNode | |
TInsertable | never | ||
ImplicitlyConstructable | boolean | boolean | |
Info | unknown | ||
TConstructorExtra | never |
Remarks
Instances of this class are nodes in the tree. This is also a constructor so that it can be subclassed.
Using classes in this way allows introducing a named type and a named value at the same time, helping keep the runtime and compile time information together and easy to refer to un a uniform way. Additionally, this works around https://github.com/microsoft/TypeScript/issues/55832 which causes similar patterns with less explicit types to infer "any" in the d.ts file.
When sub-classing a a TreeNodeSchemaClass
, some extra rules must be followed:
- Only ever use a single class from the schema's class hierarchy within a document and its schema. For example, if using object(name, fields) you can do:
// Recommended "customizable" object schema pattern.
class Good extends schemaFactory.object("A", {
exampleField: schemaFactory.number,
}) {
public exampleCustomMethod(): void {
this.exampleField++;
}
}
But should avoid:
// This by itself is ok, and opts into "POJO mode".
const base = schemaFactory.object("A", {});
// This is a bad pattern since it leaves two classes in scope which derive from the same SchemaFactory defined class.
// If both get used, its an error!
class Invalid extends base {}
- Do not modify the constructor input parameter types or values:
class Invalid extends schemaFactory.object("A", {
exampleField: schemaFactory.number,
}) {
// This Modifies the type of the constructor input.
// This is unsupported due to programmatic access to the constructor being used internally.
public constructor(a: number) {
super({ exampleField: a });
}
}