TreeNodeSchemaClass Interface

Packages > @fluidframework/tree > TreeNodeSchemaClass

Tree node schema which is implemented using a class.

Signature

/** @sealed */
export interface TreeNodeSchemaClass<out Name extends string = string, out Kind extends NodeKind = NodeKind, out TNode = unknown, in TInsertable = never, out ImplicitlyConstructable extends boolean = boolean, out Info = unknown> extends TreeNodeSchemaCore<Name, Kind, ImplicitlyConstructable, Info>

Extends: TreeNodeSchemaCore <Name, Kind, ImplicitlyConstructable, Info>

Type Parameters

Parameter Constraint Default Description
Name string string
Kind NodeKind NodeKind
TNode unknown
TInsertable never
ImplicitlyConstructable boolean boolean
Info unknown

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 });
	}
}

Construct Signatures

ConstructSignature Modifiers Return Type Description
new (data: TInsertable | InternalTreeNode): Unhydrated<TNode> sealed Unhydrated<TNode> Constructs an Unhydrated node with this schema.

Construct Signature Details

new (data: TInsertable | InternalTreeNode): Unhydrated<TNode>

Constructs an Unhydrated node with this schema.

Signature

/** @sealed */
new (data: TInsertable | InternalTreeNode): Unhydrated<TNode>;

Remarks

This constructor is also used internally to construct hydrated nodes with a different parameter type. Therefore, overriding this constructor with different argument types is not type-safe and is not supported.

Parameters

Parameter Type Description
data TInsertable | InternalTreeNode

Returns

Return type: Unhydrated <TNode>