fix: objectKeys should always return subset of string - #42
fix: objectKeys should always return subset of string#42max-programming wants to merge 1 commit into
Conversation
|
Hello @max-programming, Thanks for the PR. Good catch, there is a bug indeed however the PR does not adress the core of the issue. What we need is to convert What you are doing instead is to just strip away from the return type any keys that would be declared as a number and this isn't correct. Let me know if you are interested in solving this little typeing challenge. If you do, make sure you write the correct type level test in |
|
@garronej Yea I agree that the return type should include the literal keys. But the issue here is that the type that is passed in through generic is of So either we have to override the passed in type using If I as a consumer, make an object of let's say type Here's an example: const obj = {
someKey: "someValue"
} // Type has key as "someKey" (inferred)
const obj2: { someKey: unknown } = {
someKey: "someValue"
} // Type has key as "someKey" (manually set)
const obj3: Record<string, unknown> = {
someKey: "someValue"
} // Type has key as any string (manually set)
const o1Keys = objectKeys(obj) // ["someKey"]
const o2Keys = objectKeys(obj2) // ["someKey"]
// No way to get the keys if the type is too flexible
const o3Keys = objectKeys(obj3) // string[] (better than having (string | number)[])
// If manually given the type
const o3KeysBetter = objectKeys<{ someKey: unknown }>(obj3) // ["someKey"]
// Best approach
const o3KeysMuchBetter = objectKeys(obj3 as const) // ["someKey"]I think we should keep the default Let me know if this sounds like a reasonable solution ps: i typed all this on mobile and will update in another comment if this |
Fixes #26