简体   繁体   English

如何声明两个数字元组的返回类型?

[英]How do I declare a return type of a tuple of two numbers?

When I declare a function as当我将函数声明为

const coordinates = (id: number): ([number, number]) => {

the error I get is [ts] Duplicate identifier 'number'.我得到的错误是[ts] Duplicate identifier 'number'.

If I omit type signature for return value, then it infers it as number[]如果我省略返回值的类型签名,那么它会将其推断为number[]

const coordinates = (id: number): [number, number] => [id, id];

No need for the parenthesis around the return tuple type不需要返回元组类型周围的括号

const coordinates = (id: number) => [id, id] as const;
// const coordinates: (id: number) => [number, number]

From TypeScript 3.4 onwards you can use const assertations.从 TypeScript 3.4 开始,您可以使用 const 断言。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM