Part one asks the question - "Is it possible for TypeScript to ensure that a parameter is of a given type or its derivatives, rather than an instance of a type or its derivatives?"
TypeScript type checking on type rather than instance
The solution in part one is to use the typeof keyword
Example
function giveMeAType(type: typeof Foo): void {
}
or with generics
function giveMeAType<T extends typeof Foo>(type: T): void {
}
Expanding on this solution, I have a new problem when constructors are introduced to derived types
class Mangler<T> {
protected constructor(
public readonly item: T) {
}
public static getManglers(mangler: typeof Mangler): typeof Mangler[] {
var whatever = [];
return whatever;
}
}
class StringMangler extends Mangler<string> {
// No constructor override here...
}
class NumberMangler extends Mangler<number> {
// But in this case, I need one...
private constructor(
public readonly item: number) {
super(item);
}
}
// This one works while StringMangler does not override the constructor.
Mangler.getManglers(StringMangler);
// But when I override the constructor, it doesn't work.
Mangler.getManglers(NumberMangler);
How then do me maintain Type checking with constructor overrides?
P.S. I want the derived types to have the ability to have private or protected constructors!
Update 1
To elaborate on Nitzan Tomer's answer, I can make the constructor protected, and the error goes away, but I can't make the constructor polymophic...
class NumberMangler extends Mangler<number> {
// This polymorphic constructor doesn't work.
public constructor(
public readonly item: number,
public readonly name: string) {
super(item);
}
}
Mangler.