-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Closed
Description
I have a problem where if I use a generic function which extends from another generic type, the function parameter's type doesn't seem to be respected.
TypeScript Version:
1.8.9 (current stable)
Code
// Generic Tree structure (couldn't achieve the same with interfaces / classes)
type Tree<T> = T & {
children?: Tree<T>[];
}
interface INode
{
id: number;
}
var myTree: Tree<INode> = {
id: 1,
children: [
{
id: 2
}
]
};
// Compiles and works nicely:
console.log(myTree.id);
console.log(myTree.children[0].id);
class MyClass
{
public static displayTree1<T extends Tree<any>>(tree: T)
{
// Compile time error "Property 'children does' not exist on type 'T'"
console.log(tree.children);
}
public static displayTree2<T extends Tree<INode>>(tree: T)
{
// Compiles fine
console.log(tree.children);
}
public static displayTree3(tree: Tree<any>)
{
// Compiles fine
console.log(tree.children);
}
}
Expected behavior:
I expect the "displayTree1" function to compile fine, T extends Tree so it can be assumed to have a 'children' property.
Actual behavior:
Compile time error: Property 'children does' not exist on type 'T'.
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScript