'Tuple inference via literal typing (i.e brute force typing)
This seems so obvious and simple, but I can't get it to work. Would love to get everybody's perspective to see if you can find what am I missing.
const objA: {foo:string} = { foo:'bar' };
const objB: {foo:string} = { foo: 'bar'};
fn([
[objA, (val) => {}],
// ^^^^ type is corrent.
// ^^^ expect type to be {foo:string} (same as position 1), but got `unknown`
[objB, (val) => {}],
// IBID
]);
declare function fn<
Ax,
Bx,
A extends entry_T<Ax>,
B extends entry_T<Bx>,
>(args: [A, B?]): void;
type entry_T<X> = [X, (val: X) => void];
Solution 1:[1]
Might be another case of type inference not working through multiple levels of generics. Pretty sure I have seen issues about that on Github, but they are hard to find again.
If it is not too much of a hassle, you could just drop the extra layer that wraps the tuple:
declare function fn<A, B>(args: [[A, Fun<A>], [B, Fun<B>]?]): void;
type Fun<X> = (val: X) => void;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | H.B. |
