2

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

Pal Buff
  • 21
  • 3

1 Answers1

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]>;
}
sam256
  • 1,291
  • 5
  • 29