How can I specify to the type checker that tuples must consist of a function and valid arguments? For example:
let expressions:TYPE[] = [
[(a:number, b:string)=>{},1,"ok"], // ok
[(a:number)=>{},true] // error
[(a:number)=>{}] // error
[(a:number, b:string)=>{},1] // error
]
The current solution I have feels redundant because it uses a function:
expression<F extends (...args:any[])=>any>(fn:F, ...params:Parameters<F>){
return [fn,...params]
}
let queue:ReturnType<expression>[] = [
expression(...),
expression(...),
expression(...),// seriously?
]
If only I could do it in one call:
let queue = expressions(
[(a:number)=>{},1], // ok
[(a:number)=>{},true] // error
[(a:number)=>{}] // error
)
But creating that function is beyond my capabilities. It seems to require a generic tuple type that I don't think is possible, leading me to use the expression
function in the first place.
Here's an inadequate tuple type that doesn't work and requires extra effort:
type Expression<F extends (...args:any[])=>any = (...args:any[])=>any> = [F,...Parameters<F>]
let queue:Expression[] = [
[(a:number)=>{},true] // No error
]
// A cumbersome alternative:
const fn = (a:number)=>{}
queue = [
[fn,true] as Expression<fn> // Error as desired, but c'mon.
]