79349623

Date: 2025-01-12 09:12:40
Score: 1.5
Natty:
Report link

I have no idea why you want to write [...T], if it is equivalent to T.

So if you replace [...T] with T, it will work.

See:

interface A {
  a: string;
}

interface B {
  b: string;
}

type AorB = A | B;

function isA(arg: AorB): arg is A {
  return 'a' in arg;
}

function haveSameValue<T extends any[]>(objects: T, keys: (keyof T[number])[]): boolean {
  return objects
    .slice(1)
    .every((obj) => keys.every((key) => obj[key] === objects[0][key]));
}

function foo<T extends AorB[]>(args: T) {
  if (args.every(isA)) {
    if (haveSameValue(args, ['b'])) {
    }
  }
}

Playground

Technically, the two are not completely the same, but does it matter in this case?

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Ends in question mark (2):
  • Low reputation (0.5):
Posted by: Janek Eilts