Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

221
Views
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'.
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!