Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 获取typescript中的最后一个函数参数_Node.js_Typescript_Destructuring - Fatal编程技术网

Node.js 获取typescript中的最后一个函数参数

Node.js 获取typescript中的最后一个函数参数,node.js,typescript,destructuring,Node.js,Typescript,Destructuring,我试图对只关心最后传入的参数的函数的参数进行解构,将其值绑定到变量next;在纯js节点10中,以下操作非常有效: > function f(...{length, [length - 1]: next}) { console.log(next) } > f(1,2,3,4) 4 但是,typescript中的同一构造给了我: error TS2501: A rest element cannot contain a binding pattern. function (...{

我试图对只关心最后传入的参数的函数的参数进行解构,将其值绑定到变量
next
;在纯js节点10中,以下操作非常有效:

> function f(...{length, [length - 1]: next}) { console.log(next) }
> f(1,2,3,4)
4
但是,typescript中的同一构造给了我:

error TS2501: A rest element cannot contain a binding pattern.

function (...{length, [length - 1]: next}) {
             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

如何解决这个问题?

这是一个非常有趣的问题,因为“TS是JS的超集”这句话在这里根本不起作用

事实证明,当我们试图在销毁和扩展运算符中同时使用绑定模式时,我们会得到一个错误:

当前在TS诊断消息文件中,有一个针对的显式错误

此外,还有一个和类型脚本回购协议,它可能会解决这个“问题”(不确定这是一个问题还是其他问题)

但您始终可以为您的任务选择此方法:

function f(...args) {
    console.log(args[args.length - 1]);
}
fwiw,(…args)=>{const{length,[length-1]:next}=args}编译得很好