参数列表中的数组解构,使用 TypeScript [英] Array destructuring in parameters list, with TypeScript

查看:37
本文介绍了参数列表中的数组解构,使用 TypeScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 TypeScript,我试图弄清楚如何在参数列表中进行数组解构.

Using TypeScript, I am trying to figure out how to do array destructuring in the arguments list.

我们可以像这样使用对象解构:

We can use object destructuring like so:

let foo = function({firstname, lastname}){...}

foo({
  firstname: 'ralph',
  lastname: 'lauren'
});

我想知道我们是否可以用数组解构做同样的事情,这对我来说非常有用,比如:

I am wondering if we can do the same thing with array destructuring, it would be very useful for me, something like:

let bar = function([desc, opts, fn]){...}

bar([
  'yes',
   {},
   function(){}
]);

是否可以使用带有 TypeScript 的数组来做到这一点?

is it possible to do this with an array with TypeScript?

推荐答案

固定长度和类型的数组在 TS 中也称为 tuple.我们可以像这样解构元组参数:

An array of fixed length and types is also called a tuple in TS. We can destructure a tuple argument like:

let bar = function ([desc, opts, fn]: [string, {}, Function]) {


}

bar([
    'yes',
    {},
    function () { }
]);

这篇关于参数列表中的数组解构,使用 TypeScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆