I managed to type the object currentProps
so TS knows which properties it haves AND each property has its individual type (not an union of all possible types).
So far so good.
Then I have this generic function overrideForIndex
which gets one of the possible properties, and should return its value. But it can’t be assigned, because its return-type is a union, and not the specific type.
Now I could just cast it as any and call it a day, but I am curious if there is a way to handle this properly without casting to any
.
Here is the “simplified” example: (open in TS playground)
export type MyProps = { id?: number; title?: string; } const props: Record<keyof MyProps, any> = { id: 123, title: 'foo' } export const propNames = Object.keys(props) as Array<keyof MyProps>; const data: Record<number, MyProps> = { 0: { id: 123, title: 'foo' }}; const buildLatestProps = (): { [P in keyof Required<MyProps>]: MyProps[P] } => { const getLatest = <T extends keyof MyProps>(propName: T) => data[0][propName]; return Object.fromEntries(propNames.map(n => [n, getLatest(n)])) as any; }; const currentProps = buildLatestProps(); const overrideForIndex = <T extends keyof MyProps>(propName: T, index: number): MyProps[T] => data[index][propName]; propNames.forEach(n => (currentProps[n] = overrideForIndex(n, 0) /* as any */)); // ERR: Type 'string | number | undefined' is not assignable to type 'undefined'.
Advertisement
Answer
If you take a look at these lines, the types are acting how you are expecting them to.
currentProps["id"] = overrideForIndex("id", 0); currentProps["title"] = overrideForIndex("title", 0);
However, when you use the forEach loop, you are effectively using the following, where n
is the union type of the key names of MyProps
:
propNames.forEach((n: keyof MyProps) => { currentProps[n] = overrideForIndex(n, 0) });
Currently, TypeScript cannot untangle this. In addition, there is a great thread on why such a change could lead to unintentional consequences.
You should avoid using any
in 99% of “use cases” for it as you are effectively turning TypeScript off. In most instances where you would use any
, Record<KeyType, ValueType>
is sufficient like so:
propNames.forEach(n => ((currentProps as Record<typeof n, unknown>)[n] = overrideForIndex(n, 0)));
In that block, you effectively suppress the error without relying on turning TypeScript completely off. It will also throw an error if you did type propNames
as Array<keyof MyTypeExtendingMyProps>
.