Types for Interface Instance node fixed.

main
Jackson Morgan 9 months ago
parent 7716e49e1a
commit 637a517b4b
  1. 10
      packages/type-traverser/src/instanceGraph/nodes/InstanceNode.ts
  2. 14
      packages/type-traverser/src/instanceGraph/nodes/InterfaceInstanceNode.ts
  3. 2
      packages/type-traverser/src/instanceGraph/nodes/UnionInstanceNode.ts
  4. 14
      packages/type-traverser/src/instanceGraph/nodes/createInstanceNodeFor.ts

@ -15,7 +15,7 @@ export abstract class InstanceNode<
readonly typeName: TypeName; readonly typeName: TypeName;
protected readonly parents: Record< protected readonly parents: Record<
string, string,
Set<InstanceNodeFor<Types, Types[ParentIdentifiers<Types, TypeName>[0]]>> Set<InstanceNodeFor<Types, ParentIdentifiers<Types, TypeName>[0]>>
>; >;
constructor( constructor(
@ -37,7 +37,7 @@ export abstract class InstanceNode<
public _setParent<Identifiers extends ParentIdentifiers<Types, TypeName>>( public _setParent<Identifiers extends ParentIdentifiers<Types, TypeName>>(
identifiers: Identifiers, identifiers: Identifiers,
parentNode: InstanceNodeFor<Types, Types[Identifiers[0]]>, parentNode: InstanceNodeFor<Types, Identifiers[0]>,
) { ) {
const parentKey = this.getParentKey(identifiers); const parentKey = this.getParentKey(identifiers);
if (!this.parents[parentKey]) this.parents[parentKey] = new Set(); 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 * Returns all nodes that are children of this node reguardless of their edge
*/ */
public abstract allChildren(): InstanceNode< public abstract allChildren(): InstanceNodeFor<Types, any>[];
Types,
keyof Types,
Types[keyof Types]
>[];
protected abstract _recursivelyBuildChildren(): void; protected abstract _recursivelyBuildChildren(): void;

@ -39,7 +39,7 @@ export class InterfaceInstanceNode<
public allChildren(): InstanceNodeFor< public allChildren(): InstanceNodeFor<
Types, Types,
Types[Type["properties"][keyof Type["properties"]]] Type["properties"][keyof Type["properties"]]
>[] { >[] {
return Object.values(this.children).flat(); return Object.values(this.children).flat();
} }
@ -49,17 +49,19 @@ export class InterfaceInstanceNode<
([propertyName, value]: [keyof Type["properties"], unknown]) => { ([propertyName, value]: [keyof Type["properties"], unknown]) => {
const initChildNode = (val: unknown) => { const initChildNode = (val: unknown) => {
const node = this.graph.getNodeFor(val, this.typeName); 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 // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore // @ts-ignore
node._setParent([this.typeName, propertyName], this); node._setParent([this.typeName, propertyName], this);
return node; return node;
}; };
const childNode = ( const childNode = (Array.isArray(value)
Array.isArray(value)
? value.map((val) => initChildNode(val)) ? value.map((val) => initChildNode(val))
: initChildNode(value) : initChildNode(value)) as unknown as InterfacePropertyNode<
) as InterfacePropertyNode<Types, Type, keyof Type["properties"]>; Types,
Type,
keyof Type["properties"]
>;
this._setChild(propertyName, childNode); this._setChild(propertyName, childNode);
}, },
); );

@ -18,7 +18,7 @@ export class UnionInstanceNode<
return this.childNode; return this.childNode;
} }
public allChildren(): InstanceNode<Types, keyof Types, Types[keyof Types]>[] { public allChildren(): InstanceNodeFor<Types, Type["typeNames"]>[] {
return this.childNode ? [this.childNode] : []; return this.childNode ? [this.childNode] : [];
} }
protected _recursivelyBuildChildren() { protected _recursivelyBuildChildren() {

@ -13,13 +13,15 @@ import { UnionInstanceNode } from "./UnionInstanceNode";
export type InstanceNodeFor< export type InstanceNodeFor<
Types extends TraverserTypes<any>, Types extends TraverserTypes<any>,
TypeName extends keyof Types, TypeName extends keyof Types,
> = Types[TypeName] extends InterfaceType<keyof Types> > = {
? InterfaceInstanceNode<Types, TypeName, Types[TypeName]> [TN in TypeName]: Types[TN] extends InterfaceType<keyof Types>
: Types[TypeName] extends UnionType<keyof Types> ? InterfaceInstanceNode<Types, TN, Types[TN]>
? UnionInstanceNode<Types, TypeName, Types[TypeName]> : Types[TN] extends UnionType<keyof Types>
: Types[TypeName] extends PrimitiveType ? UnionInstanceNode<Types, TN, Types[TN]>
? PrimitiveInstanceNode<Types, TypeName, Types[TypeName]> : Types[TN] extends PrimitiveType
? PrimitiveInstanceNode<Types, TypeName, Types[TN]>
: never; : never;
}[TypeName];
export function createInstanceNodeFor< export function createInstanceNodeFor<
Types extends TraverserTypes<any>, Types extends TraverserTypes<any>,

Loading…
Cancel
Save