Types when destructuring arrays

后端 未结 5 1791
感动是毒
感动是毒 2020-12-14 05:21
function f([a,b,c]) {
  // this works but a,b and c are any
}

it\'s possible write something like that?

function f([a: number,b: nu         


        
5条回答
  •  南笙
    南笙 (楼主)
    2020-12-14 06:05

    Yes, it is. In TypeScript, you do it with types of array in a simple way, creating tuples.

    type StringKeyValuePair = [string, string];
    

    You can do what you want by naming the array:

    function f(xs: [number, number, number]) {}
    

    But you wouldn't name the interal parameter. Another possibility is use destructuring by pairs:

    function f([a,b,c]: [number, number, number]) {}
    

提交回复
热议问题