This my setup:
Define properties
interface Props<T, X extends any[], Y extends any[]> {
T?: T
x?: <D extends T>(d: D, ...args: X) => number | Date | string
y?: <D extends T>(d: D, ...args: Y) => number | Date | string
}
Define Component which consumes Props interface
class Component<T, X extends any[], Y extends any[]> {
_props: Props<T, X, Y>
constructor(props:Props<T, X, Y>){
this._props = props
}
}
Instantiate Component (I want the component to infer the generic types from props)
const comp = new Component({
T:{a:10, b:20},
x:(d, g:number)=> d.a
})
The problem I am facing is when I use the x (or y) property the following error will be printed
Type '<D extends { a: number; b: number; }>(d: D, g: number) => number' is not assignable to type '<D extends { a: number; b: number; }>(d: D, g: number) => string | number | Date'.
Types of parameters 'd' and 'd' are incompatible.
Type 'D' is not assignable to type 'D'. Two different types with this name exist, but they are unrelated.
'D' is assignable to the constraint of type 'D', but 'D' could be instantiated with a different subtype of constraint '{ a: number; b: number; }'.
Type '{ a: number; b: number; }' is not assignable to type 'D'.
'{ a: number; b: number; }' is assignable to the constraint of type 'D', but 'D' could be instantiated with a different subtype of constraint '{ a: number; b: number; }'.
Here is the full code in Typescript Playground
what I want is to infer the rest parameters type and use it later, so please if anybody has a suggestion on how to overcome this issue that will be a great help
Dat all? What's the difference between what you're doing here and this code which just accepts a param of typeT? If this turns out to be a bug or limitation in the TS compiler, the report probably needs to show a use case that motivates it, and right now there doesn't seem to be a reason forxoryto be generic methods<T extends X>(x: T)=>voidhas no reason to be generic, since it can be replaced with(x: X)=>void. On the other hand, a function type like<T extends X>(x: T) => Tor<T extends X>(x: T, cb: (x: T)=>void)=>voidor something that mentionsTat least two times cannot be replaced with a non-generic version without losing precision. If I raise a GitHub issue I'll probably just do something like this