From 637a517b4befe715a74170a535ff94f9729848a8 Mon Sep 17 00:00:00 2001 From: Jackson Morgan Date: Fri, 13 Dec 2024 17:41:49 -0500 Subject: [PATCH] Types for Interface Instance node fixed. --- .../src/instanceGraph/nodes/InstanceNode.ts | 10 +++------- .../instanceGraph/nodes/InterfaceInstanceNode.ts | 16 +++++++++------- .../src/instanceGraph/nodes/UnionInstanceNode.ts | 2 +- .../instanceGraph/nodes/createInstanceNodeFor.ts | 16 +++++++++------- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/packages/type-traverser/src/instanceGraph/nodes/InstanceNode.ts b/packages/type-traverser/src/instanceGraph/nodes/InstanceNode.ts index 8e46865..3a547bb 100644 --- a/packages/type-traverser/src/instanceGraph/nodes/InstanceNode.ts +++ b/packages/type-traverser/src/instanceGraph/nodes/InstanceNode.ts @@ -15,7 +15,7 @@ export abstract class InstanceNode< readonly typeName: TypeName; protected readonly parents: Record< string, - Set[0]]>> + Set[0]>> >; constructor( @@ -37,7 +37,7 @@ export abstract class InstanceNode< public _setParent>( identifiers: Identifiers, - parentNode: InstanceNodeFor, + parentNode: InstanceNodeFor, ) { const parentKey = this.getParentKey(identifiers); if (!this.parents[parentKey]) this.parents[parentKey] = new Set(); @@ -66,11 +66,7 @@ export abstract class InstanceNode< /** * Returns all nodes that are children of this node reguardless of their edge */ - public abstract allChildren(): InstanceNode< - Types, - keyof Types, - Types[keyof Types] - >[]; + public abstract allChildren(): InstanceNodeFor[]; protected abstract _recursivelyBuildChildren(): void; diff --git a/packages/type-traverser/src/instanceGraph/nodes/InterfaceInstanceNode.ts b/packages/type-traverser/src/instanceGraph/nodes/InterfaceInstanceNode.ts index dd70bf6..5ae057e 100644 --- a/packages/type-traverser/src/instanceGraph/nodes/InterfaceInstanceNode.ts +++ b/packages/type-traverser/src/instanceGraph/nodes/InterfaceInstanceNode.ts @@ -39,7 +39,7 @@ export class InterfaceInstanceNode< public allChildren(): InstanceNodeFor< Types, - Types[Type["properties"][keyof Type["properties"]]] + Type["properties"][keyof Type["properties"]] >[] { return Object.values(this.children).flat(); } @@ -49,17 +49,19 @@ export class InterfaceInstanceNode< ([propertyName, value]: [keyof Type["properties"], unknown]) => { const initChildNode = (val: unknown) => { const node = this.graph.getNodeFor(val, this.typeName); - // I know it's bad, I just can't figure out what's wrong with this type + // Fancy typescript doesn't work until you actually give it a type // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore node._setParent([this.typeName, propertyName], this); return node; }; - const childNode = ( - Array.isArray(value) - ? value.map((val) => initChildNode(val)) - : initChildNode(value) - ) as InterfacePropertyNode; + const childNode = (Array.isArray(value) + ? value.map((val) => initChildNode(val)) + : initChildNode(value)) as unknown as InterfacePropertyNode< + Types, + Type, + keyof Type["properties"] + >; this._setChild(propertyName, childNode); }, ); diff --git a/packages/type-traverser/src/instanceGraph/nodes/UnionInstanceNode.ts b/packages/type-traverser/src/instanceGraph/nodes/UnionInstanceNode.ts index 586390a..b28b877 100644 --- a/packages/type-traverser/src/instanceGraph/nodes/UnionInstanceNode.ts +++ b/packages/type-traverser/src/instanceGraph/nodes/UnionInstanceNode.ts @@ -18,7 +18,7 @@ export class UnionInstanceNode< return this.childNode; } - public allChildren(): InstanceNode[] { + public allChildren(): InstanceNodeFor[] { return this.childNode ? [this.childNode] : []; } protected _recursivelyBuildChildren() { diff --git a/packages/type-traverser/src/instanceGraph/nodes/createInstanceNodeFor.ts b/packages/type-traverser/src/instanceGraph/nodes/createInstanceNodeFor.ts index c5d3677..6013694 100644 --- a/packages/type-traverser/src/instanceGraph/nodes/createInstanceNodeFor.ts +++ b/packages/type-traverser/src/instanceGraph/nodes/createInstanceNodeFor.ts @@ -13,13 +13,15 @@ import { UnionInstanceNode } from "./UnionInstanceNode"; export type InstanceNodeFor< Types extends TraverserTypes, TypeName extends keyof Types, -> = Types[TypeName] extends InterfaceType - ? InterfaceInstanceNode - : Types[TypeName] extends UnionType - ? UnionInstanceNode - : Types[TypeName] extends PrimitiveType - ? PrimitiveInstanceNode - : never; +> = { + [TN in TypeName]: Types[TN] extends InterfaceType + ? InterfaceInstanceNode + : Types[TN] extends UnionType + ? UnionInstanceNode + : Types[TN] extends PrimitiveType + ? PrimitiveInstanceNode + : never; +}[TypeName]; export function createInstanceNodeFor< Types extends TraverserTypes,