79335945

Date: 2025-01-07 12:15:12
Score: 0.5
Natty:
Report link

I’m not entirely sure if enforcing this type-check is possible. It might be, but I don’t know how to achieve it.

Since you’ll probably use a workaround anyway, here’s a much simpler approach you can try:

const construct = <R>(f1: () => R, f2: (x: R) => void) => ({
  f1,
  f2,
});

const result = construct(
  () => 'string',
  (x) => {}
  // ^ x: string
);

Playground

If you prefer to declare as an object you can do so as well:

const construct = <R>(obj: { f1: () => R; f2: (x: R) => void }) => obj;

construct({
  f1: () => true as const,
  f2: (x) => {},
  // ^ x: true
});

Playground

Reasons:
  • Blacklisted phrase (1): how to achieve
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: Janek Eilts