Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

90
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda