Skip to content

allow type parameters on type arguments #5959

Closed
@zpdDG4gta8XKpMCd

Description

@zpdDG4gta8XKpMCd
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));

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.SuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions