问题
I've looked at all the following:
Abstract constructor type in TypeScript
How does `type Constructor<T> = Function & { prototype: T }` apply to Abstract constructor types in TypeScript?
Abstract Constructor on Abstract Class in TypeScript
The third is the closest to what I'm looking for, but (unfortunately) the answer was more for the specific issue and less for the question title.
This is (in a simplified sense) what I'd like to be able to do:
abstract class HasStringAsOnlyConstructorArg {
abstract constructor(str: string);
}
class NamedCat extends HasStringAsOnlyConstructorArg {
constructor(name: string) { console.log(`meow I'm ${name}`); }
}
class Shouter extends HasStringAsOnlyConstructorArg {
constructor(phrase: string) { console.log(`${phrase}!!!!!!`); }
}
const creatableClasses: Array<typeof HasStringAsOnlyConstructorArg> = [NamedCat, Shouter];
creatableClasses.forEach(
(class: typeof HasStringAsOnlyConstructorArg) => new class("Sprinkles")
);
In the example above you can see that Shouter and NamedCat both use one single string for their constructor. They don't necessarily need to extend a class, they could implement an interface or something, but I really want to be able to hold a list of classes that require the exact same arguments to construct.
Is it possible to enforce a classes constructor parameter types with extends
or implements
in TypeScript?
EDIT: The "Possible Duplicate" appears to show how it is not possible to use new()
in an interface for this purpose. Perhaps there are still other ways.
回答1:
You can do such enforcement on array itself, so it will allow only constructors with single string argument:
class NamedCat {
constructor(name: string) { console.log(`meow I'm ${name}`); }
}
class Shouter {
constructor(phrase: string) { console.log(`${phrase}!!!!!!`); }
}
type ConstructorWithSingleStringArg = new (args: string) => any;
const creatableClasses: Array<ConstructorWithSingleStringArg> = [NamedCat, Shouter];
creatableClasses.forEach(
ctor => new ctor("Sprinkles")
);
Playground
来源:https://stackoverflow.com/questions/52350788/is-it-possible-to-enforce-constructor-parameter-types-with-extends-or-impleme