Open xhd2015 opened 3 months ago
Asked on stackoverflow: https://stackoverflow.com/questions/78640175
But it seems the error is not caused by returning, but that data type is incompitable with T:
function findData<T extends { children?: T[] }>(data: T){
}
interface MyItem {
children: MyItem[]
}
// bad
function testFind<T extends {children: T[]}>(data: T) {
findData(data)
}
// good
function testFindV2<T extends MyItem>(data: T) {
findData(data)
}
But if define MyItem
as :
interface MyItem<T extends {children:T[]}> {
children: T[]
}
Everything works fine.
I have a function named
copyItem
, which accepts a generic constrained param T. But when it is called with another generic type, ts emits the error as described in the title.Function definition:
Test Code:
Got this error:
The workaround I came up with is to declare an extra generic type V, which extends T, and replace T with V in the code, demonstrated as below:
Test OK Now:
(The whole example can be found at TS Playground.)
But why?