Skip to content

Typed ...rest parameters with generics #1024

Closed
@andrewvarga

Description

@andrewvarga

Is it possible to add type info to the rest parameters in a way that each individual parameter can have a different type?

This works, but with this there can be any number parameters, and all have to have the same type:

function myFunction<T>(...args:T[]) {
}
myFunction<number>(1, 3);

It would be really useful if I could force the exact number of arguments and the types for each one, but all this within generics, so something like this (but this is obviously syntactically wrong):

function myFunction<T>(...args:T) {
}

interface MyParams {
    [0]:string;
    [1]:number;
}

myFunction<MyParams>("a", 1)

An example use case is writing an observer class that the user could specify the arguments of the listener functions for:

class Observer<T> {
    addListener(...args:T) {
    }
}

If this is not supported, what do you recommend using instead? I can use any[] of course, or living with the constraint of having a fixed number of fixed typed parameters.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already createdFixedA PR has been merged for this issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions