Skip to main content

WithType Interface

Adds a type symbol to a type for stronger typing.

Sealed

This type is "sealed," meaning that code outside of the library defining it should not implement or extend it. Future versions of this type may add members or make typing of readonly members more specific.

Signature

/** @sealed */
export interface WithType<out TName extends string = string, out TKind extends NodeKind = NodeKind, out TInfo = unknown>

Type Parameters

ParameterConstraintDefaultDescription
TNamestringstringSame as TreeNodeSchema's "Name" parameter.
TKindNodeKindNodeKindSame as TreeNodeSchema's "Kind" parameter.
TInfounknownSame as TreeNodeSchema's "Info" parameter: format depends on the Kind.

Remarks

Powers TreeNode's strong typing setup.

Example

Narrow types for overloading based on NodeKind

function getKeys(node: TreeNode & WithType<string, NodeKind.Array>): number[];
function getKeys(node: TreeNode & WithType<string, NodeKind.Map | NodeKind.Object>): string[];
function getKeys(node: TreeNode): string[] | number[];
function getKeys(node: TreeNode): string[] | number[] {
const schema = Tree.schema(node);
switch (schema.kind) {
case NodeKind.Array: {
const arrayNode = node as TreeArrayNode;
const keys: number[] = [];
for (let index = 0; index < arrayNode.length; index++) {
keys.push(index);
}
return keys;
}
case NodeKind.Map:
return [...(node as TreeMapNode).keys()];
case NodeKind.Object:
return Object.keys(node);
default:
throw new Error("Unsupported Kind");
}
}

Properties

PropertyAlertsModifiersTypeDescription
[typeNameSymbol]DeprecatedreadonlyTName

Type symbol, marking a type in a way to increase type safety via strong type checking.

Use Tree.schema(theNode) for schema related runtime data access. For type narrowing, use WithType instead of the symbols directly.

[typeSchemaSymbol]readonlyTreeNodeSchemaClass<TName, TKind, TreeNode, never, boolean, TInfo>Type symbol, marking a type in a way to increase type safety via strong type checking.

Property Details

[typeNameSymbol]

Type symbol, marking a type in a way to increase type safety via strong type checking.

Use Tree.schema(theNode) for schema related runtime data access. For type narrowing, use WithType instead of the symbols directly.

This API is deprecated and will be removed in a future release.

Use typeSchemaSymbol instead.

Signature

get [typeNameSymbol](): TName;

Type: TName

Remarks

This should be redundant with typeSchemaSymbol, but is kept for improved compiler performance. If we just rely on typeSchemaSymbol, the compiler has to work much harder to distinguish schema in unions, which can cause large schema unions to hit "error TS2859: Excessive complexity comparing types".

This property is not intended for any use other than helping the compiler distinguish schema types, and should not be referenced in any code other than internal to the tree package.

[typeSchemaSymbol]

Type symbol, marking a type in a way to increase type safety via strong type checking.

Signature

get [typeSchemaSymbol](): TreeNodeSchemaClass<TName, TKind, TreeNode, never, boolean, TInfo>;

Type: TreeNodeSchemaClass<TName, TKind, TreeNode, never, boolean, TInfo>