Closed
Description
TypeScript Version: 4.0.2
Code
interface Foo {
bla: string;
}
declare function map<T, R>(value: T[], transform: (t: T) => R): R[];
declare const foos: Foo[];
declare function call<Fn extends (...args: any[]) => any>(
fn: Fn,
...args: Parameters<Fn>
): ReturnType<Fn>;
const a = call(map, foos, (foo: Foo) => foo.bla);
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'
Playground Link:
Playground
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
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 was a minute faster than me. :-D
#38964 and #37181 are also related.
kasperpeulen commentedon Sep 28, 2020
@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
, buta
is 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.