声明数组中可选的最后一个元素?

tra*_*ang 0 arrays tuples optional-parameters typescript

我想声明一个 Typescript 数组类型,其中最后一个元素是可选的。有没有什么优雅的方法来实现这一目标?

const test = (a: [string, string, any | undefined]) => console.log(a)
test(['foo', 'bar'])
Run Code Online (Sandbox Code Playgroud)

test(...)函数调用中,ts 详细说明此错误

Argument of type '[string, string]' is not assignable to parameter of type '[string, string, any]'.
  Property '2' is missing in type '[string, string]' but required in type '[string, string, any]'.(2345)
Run Code Online (Sandbox Code Playgroud)

Sha*_*son 5

const test = (a: [string, string, any?]) => console.log(a)
test(['foo', 'bar'])
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。