Search code examples
typescripttypescript-genericsmapped-typesrecordtype

How to convert object of any type values, to object of string values (Typescript)?


I have an object.

let obj1: A;
/*
type A = {
  property1: any;
  property2: any;
}
*/

I know that the values in the object are all strings, but I don't want to forcefully typecast.

// I don't want to do this
const obj2 = obj1 as Record<keyof typeof obj1, string>

Instead, I want to infer it in the right way, using typescript predicates. This is my attempt to do it.

function getIsCorrectType<T extends Record<string, any>>(
  obj: T
): obj is Record<keyof T, string>{
  return true; // assume that I manually checked each value to be a string
}

However I now get an error

A type predicate's type must be assignable to its parameter's type.
  Type 'Record<keyof T, string>' is not assignable to type 'T'.
    'Record<keyof T, string>' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'Record<string, any>'.
      Type 'string' is not assignable to type 'T[P]'.

To me this sounds crazy. I should be able to assign string to T[P] = any, right? What am I doing wrong? Is there any alternative solution to this?


Solution

  • In order to make it work, you need to infer just a set of keys instead of whole object:

    const isString = (value: unknown): value is string => typeof value === 'string'
    
    const getIsCorrectType = <K extends string>(
      obj: Record<K, unknown>
    ): obj is Record<K, string> =>
      Object.values(obj).every(isString)
    
    const x = getIsCorrectType({ 1: 'sdf' })
    
    

    K - is used for keys inference

    Also I have added isString custom typeguard