#Type 'K' is not assignable to type 'string | number | bigint | boolean'. ts(2322)

7 messages · Page 1 of 1 (latest)

acoustic jewel
#

Hello,

I'm getting this error with code:

/**
 * @template {string} S
 * @template {{ [key: string]: string }} OBJ
 * @param {OBJ} obj
 * @param {S} str
 * @returns {{
 *     [K in keyof OBJ as `${str}${K}`]: OBJ[K]       // <--- THIS DOESN'T THROW
 * }}
 */
export function example(obj, str) {
    return Object.entries(obj).reduce((acc, [key, val]) => {
        acc[str + key]: val;
        return acc;
    }, /** @type {{ [K in keyof OBJ as `${str}${K}`]: OBJ[K] }} */ ({})) // <--- THIS THROWS ERROR
    //                                          ^
}

I'm not sure what is problem, because keyof OBJ should be string.

#

Type 'K' is not assignable to type 'string | number | bigint | boolean'. ts(2322)

ember wharf
#

@acoustic jewel You need to do:

[K in (keyof OBJ & string) as `${str}${K}`]
#

OBJ could have non-string keys (symbols mostly) and it's not valid to do string interpolation with symbols.

acoustic jewel
#

thank you so much for help... again, it's just my brain malfunctioning... such easy solve of issue. But it's weird it didn't throw error in return type.

ember wharf
#

Yeah, that is a bit strange, might be some sort of minor bug with JSDoc type checking - maybe TS doesn't raise errors on the JSDoc comments themselves?

acoustic jewel
#

It mostly does for sure... I had already a lot of other errors in JSDoc before.