Fuse icon indicating copy to clipboard operation
Fuse copied to clipboard

refactor(options): improve keys option typing

Open yoavbls opened this issue 4 years ago • 3 comments

Make keys typing more precise, allow only existing object keys. The change is already covered by typing.tests.ts and pass all tests.

yoavbls avatar Jan 07 '22 13:01 yoavbls

@yoavbls Thank you so much for your contribution to this project, it means a lot to us! Generic is a great feature to use and you have done a great job, but in my opinion, replacing only any type with <T> is more precise. what do you think? thank you!

lifeeric avatar Jan 28 '22 22:01 lifeeric

Thank you @lifeeric! I think that the types that I changed to are more precise,
TypeScript will validate that the keys exist in the object and enable auto completions.
I also added a default value for the generic parameter for any case,
I can add it also to the FuseOptionKeyObject if you want to 🙂

yoavbls avatar Jan 31 '22 14:01 yoavbls

@yoavbls

I'm afraid that we can't type keys as just key of T since the strings may be property names, or a path to a property in "dot notation".

📚 See documentation for Nested Search

For example, given a list of objects like:

[
  {
    "title": "Old Man's War",
    "author": {
      "name": "John Scalzi",
      "tags": [
        {
          "value": "American"
        }
      ]
    }
  },
  {
    "title": "The Lock Artist",
    "author": {
      "name": "Steve Hamilton",
      "tags": [
        {
          "value": "English"
        }
      ]
    }
  }
]

You can pass keys as:

const options = {
  keys: ['author.tags.value']
}
const options = {
  keys: [['author', 'tags', 'value']]
}

The key names can also be generated, if using a getFn, for example:

const options = {
  keys: [
    { name: 'title', getFn: (book) => book.title },
    { name: 'authorName', getFn: (book) => book.author.name }
  ]
})

We would have to accept key: keyof T | string which would widen to just string 🤷‍♂️

BenJenkinson avatar May 06 '22 09:05 BenJenkinson