Closed
Description
interface Map<a> {
[key: string]: a;
}
// IMPORTANT: here my intention is that the `read` and `run` methods are agreed on using the same type parameter
interface Command<Options> {
read(args: string[]): Options;
run(options: Options): void;
};
interface CopyCommandOptions {
from: string;
to: string;
}
interface CleanCommandOptions {
directory: string;
pattern: string;
}
// currently in TypeScript there are 2 options to to declare `knownCommands` below:
// - Map<Command<any>> which contaminates my code with `any`
// - Map<Command<CopyCommandOptions | CleanCommandOptions>> which has different semantics
// ideally I wish I could declare it like this:
let knownCommands : Map<<a>Command<a>> = { // <-- hypothetical syntax
'copy': <Command<CopyCommandOptions>> undefined,
'clean': <Command<CleanCommandOptions>> undefined
};
// so that later I could the following do in a type safe manner:
let command = knownCommands[name];
command.run(command.read(args));