You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
given the following 2 structures, they look like the same, i.e. a parent-child relationship
interfaceCategory{children?: this[],parent?: this
}interfaceNode{items?: this[],parent?: this
}
I want to access them the same way, by giving the relative property names, e.g. 'children'/'items' for child relation and 'parent' for parent relation, like this:
letcategory: Category={};// access children and parent of categoryhandle(category,'children','parent');//!! <- inference problem hereletnode: Node={};// access items and parent of nodehandle(node,'items','parent');//!! <- inference problem here
here is how I define the handle method with some helpers
typeNullable<T>=T|undefined|null;typePickKey<T,V>=keyof{[KinkeyofT]: T[K]extendsNullable<V> ? K : never};exportfunctionhandle<TextendsRecord<PickKey<T,T[]>,Nullable<T[]>&Record<PickKey<T,T>,Nullable<T>>>>(tree: T,childrenKey: PickKey<T,T[]>,parentKey: PickKey<T,T>,){letparent=tree[parentKey];if(parent){letsiblings=parent[childrenKey];}letchildren=tree[childrenKey];if(children){for(letii=0;ii<children.length;ii++){constchild=children[ii];}}}
Expected behavior:
It should compile
Actual behavior:
Argument of type 'Category' is not assignable to parameter of type 'Record<"children" | "parent", Category[] & Record<"children" | "parent", Nullable<Category>>>'.
Types of property 'children' are incompatible.
Type 'Category[] | undefined' is not assignable to type 'Category[] & Record<"children" | "parent", Nullable<Category>>'.
Type 'undefined' is not assignable to type 'Category[] & Record<"children" | "parent", Nullable<Category>>'.
Type 'undefined' is not assignable to type 'Category[]'.ts(2345)
TypeScript Version: 3.9.5
Search Terms:
[K in keyof T]: T[K] extends V | undefined | null
type T[keyof T] does not
infer property type
Code
given the following 2 structures, they look like the same, i.e. a parent-child relationship
I want to access them the same way, by giving the relative property names, e.g. 'children'/'items' for child relation and 'parent' for parent relation, like this:
here is how I define the handle method with some helpers
Expected behavior:
It should compile
Actual behavior:
Playground Link:
Related Issues:
it is related, but not really the same
#38646
The text was updated successfully, but these errors were encountered: