I'm trying to write a signature for this function:
export function objToArray(obj){
let ret = [];
for(const key of Object.keys(obj)){
ret.push(Object.assign({objKey: key.toString()}, obj[key]));
}
return ret;
}
Array<U & {objKey: string}>
You can use lookup types. A type T
has keys of type keyof T
and values of type T[keyof T]
. Your function would therefore be typed like this:
export function objToArray<T>(obj: T): Array<T[keyof T] & { objKey: string }>{
let ret = [];
// Object.keys() returns `string[]` but we will assert as `Array<keyof T>`
for(const key of Object.keys(obj) as Array<keyof T>){
ret.push(Object.assign({objKey: key.toString()}, obj[key]));
}
return ret;
}
Hope that helps; good luck!