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
{{ message }}
Repository navigation
Can not call a generic function as an call "effect" (pattern in in redux-saga) #40798
Expected behavior:
I expect this to give no compile errors.
Actual behavior:
Argument of type '(foo: Foo) => string' is not assignable to parameter of type '(t: unknown) => unknown'. Types of parameters 'foo' and 't' are incompatible. Type 'unknown' is not assignable to type 'Foo'
This falls largely under the same design limitation as described here: #31811.
When using generics a good guideline to follow is to push generic parameters down such that they quantifier over as little structure as necessary. I think the best way to write this is:
The next step would be if the callback (the third argument), could be inferred from the first two arguments.
interfaceFoo{bla: string;}declarefunctionmap<T,R>(value: T[],transform: (t: T)=>R): R[];declareconstfoos: Foo[];declarefunctioncall<Pextendsunknown[],R>(fn: (...args: P)=>R,
...args: P): R;// Object is of type 'unknown'. vvvconsta=call(map,foos,(foo)=>foo.bla);
This fails. With the error in the comment. However, if I write it like this:
declarefunctioncall<X,Pextendsunknown[],R>(fn: (x: X, ...args: P)=>R,x: X,
...args: P): R;// Compiles, but a inferred as unknown[]consta=call(map,foos,(foo)=>foo.bla);
It does compile, if I hover over foo it is of type Foo, but a is inferred as unknown[]. Is this a "legit" bug?
Activity
jack-williams commentedon Sep 27, 2020
This falls largely under the same design limitation as described here: #31811.
When using generics a good guideline to follow is to push generic parameters down such that they quantifier over as little structure as necessary. I think the best way to write this is:
MartinJohns commentedon Sep 27, 2020
Jack Williams (@jack-williams) was a minute faster than me. :-D
#38964 and #37181 are also related.
kasperpeulen commentedon Sep 28, 2020
Jack Williams (@jack-williams) Ah that is interesting thanks!
The next step would be if the callback (the third argument), could be inferred from the first two arguments.
This fails. With the error in the comment. However, if I write it like this:
It does compile, if I hover over foo it is of type
Foo, butais inferred asunknown[]. Is this a "legit" bug?RyanCavanaugh commentedon Sep 28, 2020
Gonna have to link #30134 for that one
typescript-bot commentedon Oct 1, 2020
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.