Estoy tratando de entender los tipos en Typescript. En general, va bien, pero estoy luchando para convertir esta función de Javascript a Typescript.
export function keyed(object, key, value) { const newKey = object[key]; const newValue = object[value]; const newObject = {}; newObject[newKey] = newValue; return newObject; };Este es mi intento hasta ahora:
type ValueOf<T> = T[keyof T]; export function keyed<T, K extends keyof T>(object: T, key: K, value: K): { [key: ValueOf<T>]: ValueOf<T> } { const newKey: ValueOf<T> = object[key]; const newValue: ValueOf<T> = object[value]; const newObject: { [key: ValueOf<T>]: ValueOf<T> } = {}; newObject[newKey] = newValue; return newObject; };Pero, estoy recibiendo algunos errores como:
An index signature parameter type cannot be a literal type or generic type. Consider using a mapped object type instead.ts(1337)y
Type 'ValueOf<T>' cannot be used to index type '{}'.ts(2536)He intentado usar definiciones DefinitelyTyped de guiones bajos para ayudar aquí , ya que hacen bastante de estas operaciones, pero no he tenido suerte. ¿Es imposible?