2

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?

benifest
  • 85
  • 1
  • 6
  • 2
    Why does `register` need a parameter if there exists only one value that can be passed? – jabaa Jan 29 '22 at 14:56
  • Because i can do this:```Class OtherCommand extends Command``` – benifest Jan 29 '22 at 15:03
  • 1
    Give your `Command` class some methods so that it doesn't have the empty interface (that both `Object` and `Map` and really anything else implements). – Bergi Jan 29 '22 at 15:06
  • It's still weird though that [the `Map` constructor can be assigned to `{new (p: number): object}`](https://www.typescriptlang.org/play?#code/HYQwtgpgzgDiDGEAE8CuUAuB7MBhHYIwAJkgN4BQS1SEAHjFgE4YoA2IUUS+YhJ5KjWExUAIzYBLeCizBMTVPGxMAFHCbgIGCEwBcSYKjBjdASkFDh1AL5Wkd4fUYskAM1TBlkuUiYQAc0lMXQBBVQANAwwATxgILDceAiJiC0prGng5TCQ6JABeQwgAdyQI1QBWM3tHGmdmVg8vDB9gP0DgnSYAIUiDMmBS9QMjE3MDXn5iG3T7YWz5VnyiobKK6tqKOzRMHCnUgDp-IJCmcIBZEBga3ew8FJJjzrPwgHkxACsIZVv0e4OTxOXV0fSuNwod32j2Iz1O3T6H2+v0h-2hfCOwNeqngHC45FkS0UymYIyQCkkwACZjINlmqL2DwxQJeCJxeO4ZEJCiUKjJFKpNLpZiAA) despite accepting an iterable not a number – Bergi Jan 29 '22 at 15:23

1 Answers1

8

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.

  1. The constructor will take any first argument, undefined or not, so any class which has a constructor accepting zero or one arguments will match
  2. The signature of the object that will be returned is empty, no methods or properties exist to identify it and so any methods/props will "fall-through" and be allowed as an "extension" of your class

We 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)

aaron.huggins
  • 543
  • 3
  • 7