As described here: Declare class type with TypeScript (pass class as parameter) i can pass a class type as parameter.
There is a problem:
export namespace customCommand {
export class Command {
public constructor(parameter?: any) {
}
}
export function register(command: typeof Command) {
new command()
}
}
When i do this
customCommand.register(Map)
or
customCommand.register(Object)
There is no errors. I know that typeof Command and typeof Map both return the same result.
But how can i protect this and pass only Command type?
In TypeScript, you can construct a new type, something like:
type Class<T> = new (...args: any[]) => T
Which you can then use this way:
function register(command: Class<Command>) {
new command()
}
This should constrain TypeScript to ONLY accept classes which construct or extend the Command type.
EDIT:
Sorry, missed something when I evaluated your example. Your class Command is generic enough that almost any constructable object will overlap. What it's missing is constraints.
any first argument, undefined or not, so any class which has a constructor accepting zero or one arguments will matchWe can over come either of these by modifying either properties or methods required for the Command class, thus constraining derived types to that explicit signature
If we want to narrow the possible matches by adding a method or property specific to Command, then we get the desired behavior you've outlined.
export namespace customCommand {
export type Class<T> = new (...args: any[]) => T
export class Command {
public constructor(parameter?: any) {
}
picard (): string {
return 'Make it so.'
}
}
export function register(command: Class<Command>) {
new command()
}
}
/*
Argument of type 'MapConstructor' is not assignable to
parameter of type 'Class<Command>'. Property 'picard'
is missing in type 'Map<any, any>' but required in
type 'Command'. ts(2345)
*/
customCommand.register(Map)
/*
Argument of type 'ObjectConstructor' is not assignable to
parameter of type 'Class<Command>'. The 'Object' type is
assignable to very few other types. Did you mean to use
the 'any' type instead? Property 'picard' is missing in
type 'Object' but required in type 'Command'. ts(2345)
*/
customCommand.register(Object)
/* No error */
customCommand.register(customCommand.Command)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With