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. |
[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 typeSchemaSymbol instead.
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
get [typeNameSymbol](): TName;
Type: TName
[typeSchemaSymbol]
Type symbol, marking a type in a way to increase type safety via strong type checking.
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
get [typeSchemaSymbol](): TreeNodeSchemaClass<TName, TKind, TreeNode, never, boolean, TInfo>;
Type: TreeNodeSchemaClass<TName, TKind, TreeNode, never, boolean, TInfo>