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?
In order to make it work, you need to infer just a set of keys instead of whole object:
K
- is used for keys inferenceAlso I have added
isString
custom typeguard