flow icon indicating copy to clipboard operation
flow copied to clipboard

Add more useful context to $Keys error message

Open jsdf opened this issue 10 years ago • 1 comments

Given a usage of $Keys like:

function keyMirror<T>(
  keysObj: T & {[key: string]: null},
) : {[key: $Keys<T>]: string} {
  const mirrored = {};
  for (let key in keysObj) {
    mirrored[key] = key;
  }
  return mirrored;
}

const typeMap = keyMirror({AA: null});

console.log(typeMap.AB);

I get a type error in the keyMirror function definition:

13: console.log(typeMap.AB)
                ^ property `AB`. Property not found in
3: ) : {[key: $Keys<T>]: string} {
              ^ object literal

However the context I really want was from where T was bound, eg.

13: console.log(typeMap.AB)
                ^ property `AB`. Property not found in
11: const typeMap = keyMirror({AA: null});
                              ^ object literal

This context lets me find the invocation of keyMirror which created the specific $Keys type which my error relates to.

Some combination of the two error messages might be even better 😄

jsdf avatar Oct 28 '16 20:10 jsdf

Seems like this was improved

https://flow.org/try/#0GYVwdgxgLglg9mABAawKYE8CyMBOO44A8AKgHwAUAUIihgM4DyARgFYBcixiAZIgN4BtNOg50oOGGADmAXQ5gQAG0UBfADSUAlIg6DhHACQBpeiVJzEYidJX9qiCAjGIAtrnw5UAE0QBefioA3PbABIjkiqhQtOiIkjGMrNp89jRueATeQhgyfjHBNCr2nlAgOEjpHt7BRZSOYM5Q6AAOqJgAhs15wtgZOOR8AIKD8kqqmsF1TnCRAHSKcFLkTa0dzbODAEITQA

This probably can be closed. /cc @vkurchatkin

TrySound avatar Mar 12 '18 12:03 TrySound