-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can not call a generic function as an call "effect" (pattern in in redux-saga) #40798
Comments
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: declare function call<P extends unknown[], R>(
fn: (...args: P) => R,
...args: P
): R; |
@jack-williams was a minute faster than me. :-D |
@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. interface Foo { bla: string; }
declare function map<T, R>(value: T[], transform: (t: T) => R): R[];
declare const foos: Foo[];
declare function call<P extends unknown[], R>(
fn: (...args: P) => R,
...args: P
): R;
// Object is of type 'unknown'. vvv
const a = call(map, foos, (foo) => foo.bla); This fails. With the error in the comment. However, if I write it like this: declare function call<X, P extends unknown[], R>(
fn: (x: X, ...args: P) => R,
x: X,
...args: P
): R;
// Compiles, but a inferred as unknown[]
const a = call(map, foos, (foo) => foo.bla); It does compile, if I hover over foo it is of type |
Gonna have to link #30134 for that one |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
TypeScript Version: 4.0.2
Code
Expected behavior:
I expect this to give no compile errors.
Actual behavior:
Playground Link:
Playground
The text was updated successfully, but these errors were encountered: