Typescript: Generic function should not return union of all types

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'.