WithType Interface
Adds a type symbol to a type for stronger typing.
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
| Parameter | Constraint | Default | Description |
|---|---|---|---|
| TName | string | string | Same as TreeNodeSchema's "Name" parameter. |
| TKind | NodeKind | NodeKind | Same as TreeNodeSchema's "Kind" parameter. |
| TInfo | unknown | Same 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
| Property | Alerts | Modifiers | Type | Description |
|---|---|---|---|---|
| [typeNameSymbol] | Deprecated | readonly | TName | Type symbol, marking a type in a way to increase type safety via strong type checking. Use |
| [typeSchemaSymbol] | readonly | TreeNodeSchemaClass<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.
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>