Variadic Tuple using Spread
operator
type VariadicTuple = [ string , ...number [ ] ] ;
var vt : VariadicTuple ;
// Valid
vt = [ "str" ] ;
vt = [ "str" , 1 ] ;
vt = [ "str" , 1 , 2 , 3 ] ;
// Invalid
vt = [ "str" , 1 , 2 , 3 , "bad" ] ;
Plain typescript with minimum one item
type VariadicTupleWithMinimum = [ string , number , ...number [ ] ] ;
var vtm : VariadicTupleWithMinimum ;
// Valid
vtm = [ "str" , 1 ] ;
vtm = [ "str" , 1 , 2 , 3 ] ;
// Invalid
vtm = [ "str" ] ;
vtm = [ "str" , 1 , 2 , 3 , "bad" ] ;
import { z } from "zod" ;
const ZodVariadicTuple = z . tuple ( [ z . string ( ) ] ) . rest ( z . number ( ) ) ;
type ZodVariadicTuple = z . infer < typeof ZodVariadicTuple > ;
let zvt : ZodVariadicTuple ;
// Valid
zvt = [ "str" ] ;
zvt = [ "str" , 1 ] ;
zvt = [ "str" , 1 , 2 , 3 ] ;
// Invalid
zvt = [ "str" , 1 , 2 , 3 , "bad" ] ;
Using Zod with minimum 1 item
import { z } from "zod" ;
const ZodVariadicTupleMin = z . tuple ( [ z . string ( ) , z . number ( ) ] ) . rest ( z . number ( ) ) ;
type ZodVariadicTupleMin = z . infer < typeof ZodVariadicTupleMin > ;
let zvtm : ZodVariadicTupleMin ;
// Valid
zvtm = [ "str" , 1 ] ;
zvtm = [ "str" , 1 , 2 , 3 ] ;
// Invalid
zvtm = [ "str" ] ;
zvtm = [ "str" , 1 , 2 , 3 , "bad" ] ;