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

78
Views
How to resolve Intersection and Union doesn't match in ts?

I want to create a function that will create a different class by passing different args ,

but I am getting an error in the code below;

It looks like even though I specify the generic, typescript can't understand it

type ClassAOptions = {
  fooA: string;
  barA: string;
};
class ClassA {
  constructor(options: ClassAOptions) {}

  static new(options: ClassAOptions): ClassA {
    return new ClassA(options);
  }
}

type ClassBOptions = {
  fooB: boolean;
  barB: boolean;
};
class ClassB {
  constructor(options: ClassBOptions) {}

  static new(options: ClassBOptions): ClassB {
    return new ClassB(options);
  }
}

const classList = {
  first: ClassA,
  second: ClassB,
};

function createClass<K extends keyof typeof classList>(
  className: K,
  args: ConstructorParameters<typeof classList[K]>[0]
) {
  //  type error
  //  Argument of type 'ClassAOptions | ClassBOptions' is not assignable to parameter of type 'ClassAOptions & ClassBOptions'.
  return new classList[className](->args<-);
}

createClass("first", { fooA: "false", barA: "false" });
createClass("second", { fooB: false, barB: false });

this type error is make me crazy, does anyone know why typescript will show an error here,

I can't find my answer on google at all;

Or let me know the key point of this type err, I can google it then

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

0

The issue you are having is that Typescript will not resolve K until you actually pass in a value to createClass. In other words, it will not conditionally resolve K inside the generic function createClass, even though it is resolvable when createClass is called.

For more on this issue, see this SO answer.

You can nevertheless achieve what you want via an explicit return type (return types are resolved when the function is actually called) and an internal assertion.

function createClass<K extends keyof typeof classList>(
  className: K,
  args: ConstructorParameters<typeof classList[K]>[0]
): InstanceType<typeof classList[K]> {
  return new classList[className](args) as InstanceType<typeof classList[K]>;
}
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!