refactor(options): improve keys option typing
Make keys typing more precise, allow only existing object keys. The change is already covered by typing.tests.ts and pass all tests.
@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!
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
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 🤷♂️