Closed
Description
I'm trying to model the underscore / lodash chain method and running into an odd issue.
TypeScript Version: 2.1.4
Code
interface Chainable<T> {
value(): T;
mapValues<U>(func: (v: T[keyof T]) => U): Chainable<{[k in keyof T]: U}>;
}
declare function chain<T>(t: T): Chainable<T>;
const square = (x: number) => x * x;
const v = chain({a: 1, b: 2}).mapValues(square).value();
Expected behavior:
v
should have type {a: number, b: number}
.
Actual behavior:
v
has type {a: U, b: U}
.
It's not even really clear to me what U
means in this context. TS knows that the type of square
is (x: number) => number
, so it should be able to deduce that U
in mapValues
must be number
.
What's more, even if I make U
explicit:
const v = chain({a: 1, b: 2}).mapValues<number>(square).value();
then v
still comes out with a type of {a: U, b: U}
.