I have the code below as a generic function to get all parents in a Tree type of structure.
When I use it in a class though, I'm getting a Type Error.
type GetParentFunc<T> = (x:T)=>T;
function getAllParents<T>(obj:T, getParent:GetParentFunc<T>)
{
let result:T[] = [];
let parent = getParent(obj);
while ( parent != null )
{
result.push( parent );
parent = getParent(parent);
}
return result;
}
class Category
{
parent:Category;
// doesn't work
parentsBroken = () =>
{
return getAllParents(this, x => this.parent);
}
// works
parentsFixed = () =>
{
return getAllParents<Category>(this, x => this.parent);
}
}
T
is getting filled in with this
- and I get the following error about the x => x.parent argument.
Type 'Category' is not assignable to type 'this'.
This is easily fixable like:
getAllParents<Category>(this, x => x.parent)
getAllParents(this as Category, x => x.parent)
But I am a visual noise minimalist, and would rather spend some time so I can get rid of this noise for the foreseeable future.
Is there a way to exclude the this
Type from a generic?
Like:
export function getAllParents<T exclude this>(obj:T, getParent:Func<T>)