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. 16
      packages/type-traverser/src/instanceGraph/nodes/InterfaceInstanceNode.ts
  3. 2
      packages/type-traverser/src/instanceGraph/nodes/UnionInstanceNode.ts
  4. 16
      packages/type-traverser/src/instanceGraph/nodes/createInstanceNodeFor.ts

@ -15,7 +15,7 @@ export abstract class InstanceNode<
readonly typeName: TypeName;
protected readonly parents: Record<
string,
Set<InstanceNodeFor<Types, Types[ParentIdentifiers<Types, TypeName>[0]]>>
Set<InstanceNodeFor<Types, ParentIdentifiers<Types, TypeName>[0]>>
>;
constructor(
@ -37,7 +37,7 @@ export abstract class InstanceNode<
public _setParent<Identifiers extends ParentIdentifiers<Types, TypeName>>(
identifiers: Identifiers,
parentNode: InstanceNodeFor<Types, Types[Identifiers[0]]>,
parentNode: InstanceNodeFor<Types, Identifiers[0]>,
) {
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<Types, any>[];
protected abstract _recursivelyBuildChildren(): void;

@ -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<Types, Type, keyof Type["properties"]>;
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);
},
);

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

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

Loading…
Cancel
Save