nevware21 / ts-utils

Common JavaScript functions for TypeScript
MIT License
4 stars 1 forks source link

[Bug] objForEachKey() signature for the key is always string and not the key type #293

Closed nev21 closed 1 month ago

nev21 commented 4 months ago

The signature for the callback of the objForeachKey() specifies that the key is always a string, when it's not, it may be a string, symbol or number.

export function objForEachKey<T>(theObject: T, callbackfn: (key: string, value: T[keyof T]) => void | number, thisArg?: any): void {

So the "type" of prop is really a PropertyKey (string, number or symbol)

...
for (const prop in theObject) {
    if (objHasOwn(theObject, prop)) {
        if (callbackfn[CALL](thisArg || theObject, prop, theObject[prop]) === -1) {
...

The signature should reflect the true nature of key type and not specify that it's always a string (even if most of the time the enumerable type for the object may be)

nev21 commented 3 months ago

Changing this will introduce downstream consumption breakages, will need to stage the signature change. For now the workaround will be to just cast the value to any as the callback is called when the value is not a string.

nev21 commented 1 month ago

Actually, as this is using for..in this only returns string keys, so I'm not going to change this. If / when needed it would be better to introduce a different function which would look over all enumerable properties and not just strings. Likewise for objKeys