This can be done if we use an extra function to help type inference a bit:
function tupleArray<T1, T2, T3>(arr:[T1, T2, T3][]) : typeof arr
function tupleArray<T1, T2>(arr:[T1, T2][]) : typeof arr
function tupleArray<T1>(arr:[T1][]) : typeof arr
function tupleArray(arr:any[]) : any[]{
return arr;
}
var t = tupleArray([ ["a", 2], ["b", 2] ]) // [string, number][]
Edit
Better version with fewer overrides:
const tupleArray = <T extends ([any] | any[])[]>(args: T): T => args
tupleArray([["A", 1], ["B", 2]]) // [string, number][]
You can add more overloads if you need more then 3 items in the tuple.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…