I have created the following code snippet and verified that it is functional:
function cases<K extends string, V, U, T>(map: { [key in K]: [V, U, T] }): [K, V, U, T][];
function cases<K extends string, V, U>(map: { [key in K]: [V, U] }): [K, V, U][];
function cases<K extends string, V>(map: { [key in K]: V }): [K, V][];
function cases<K extends string, V>(map: { [key in K]: V }) {
return Object.keys(map).map(key => ([key] as any[]).concat(map[key as K]) as any);
}
for (const [key, arr, res] of cases({
"abc": [[1, 2, "qqq"], 'qwe'],
"def": [[4, 5, "asd"], 'www'],
})) {
// const key: "abc" | "def"
// const arr: (string | number)[]
// const res: string
}
However, I am trying to avoid writing multiple overloads like this:
function cases<K extends string, V, U, T>(map: { [key in K]: [V, U, T] }): [K, V, U, T][];
function cases<K extends string, V, U>(map: { [key in K]: [V, U] }): [K, V, U][];
function cases<K extends string, V>(map: { [key in K]: V }): [K, V][];
and instead want to define a tuple type:
function cases<K extends string, V extends any[]>(map: { [key in K]: V }): [K, ...V] {
Unfortunately, it fails with an error message:
A rest element type must be an array type.
How can I resolve this issue?