graphql / dataloader

DataLoader is a generic utility to be used as part of your application's data fetching layer to provide a consistent API over various backends and reduce requests to those backends via batching and caching.
MIT License
12.89k stars 515 forks source link

Use Tuples instead of ArrayLike types #273

Closed vecerek closed 3 years ago

vecerek commented 3 years ago

Assume the following code:

const loadResults = (): Promise<Array<Result | Error>> => {
  const [a, b] = loader.loadMany(["1", "2"]);

  // TypeError: "a" and "b" can both be undefined
  if (a.prop === b.prop) {
    return [a];
  }

  return [a, b];
}

In the above code, both a and b can be undefined because the ArrayLike type does not guarantee the presence of any element; basically, loadMany could still be an empty array (based on the type). However, based on the logic implemented in this library, I believe the number of results will always match the number of keys in the input. Hence, the use of tuples makes more sense, in my opinion.

What do you think?

linux-foundation-easycla[bot] commented 3 years ago

CLA Not Signed

vecerek commented 3 years ago

Closing this, my mistake. I thought variadic tuple types could express the relationship between the cardinality of arguments and the return type in a generic way but they can't 🙁