Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

Playground

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];

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading