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

275
Views
How to resolve - Argument of type is not assignable to type - TYPESCRIPT

I have a function, that takes among other things an object of functions:

type ForMembers<S> = {
 [Key: string]: (arg: S) => void;
};

export default function transform<S extends { [index: string]: any }, D extends { [index: string]: any }>(
  source: S,
  interfaceName: string,
  forMembers?: ForMembers<S>
): D {
  const newObj = extract(source, getKeys(interfaceName));
  if (forMembers) {
    Object.keys(forMembers).forEach((key) => forMembers[key](newObj));
  }
  return newObj as unknown as D;
}

The idea is that i can pass any object of functions that i want, but typescript for some reason requires that i pass all the properties that exist on type , otherwise it throws an error

For instance if D is

interface Destination {
name: string;
bio: string;
names: string[];
profession: {
  field: string;
  level: string;
  };

And i call the function as:

transform<Source, Destination>(sourceData, "Destination", {
  name: () => {},
  bio: () => {},
  profession: () => {},
});
}

It will throw an error:

Argument of type '{ name: () => void; bio: () => void; profession: () => void; }' is not assignable to parameter of type 'ForMembers<Destination, Source>'. Property 'names' is missing in type '{ name: () => void; bio: () => void; profession: () => void; }' but required in type 'ForMembers<Destination, Source>'.

If i add the missing property - 'names', the error goes away, however i want to pass only the properties i need, not all of them. So the question is this - How to make the function to take as forMembers any combination of properties of D, not necessarily all of them?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can tell TS that all properties of an object are optional, using Partial:

forMembers?: Partial<ForMembers<S>>
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!