[英]How to declare a typed record in TypeScript as return value for a function that returns a record?
I have a library that offers the following function:我有一个提供以下功能的库:
item(
id: BigNumberish,
overrides?: CallOverrides
): Promise<[number, number] & { x: number; y: number }>;
I'd like to define the return value in a const-variable in order to receive the value in a typed fashion.我想在 const 变量中定义返回值,以便以类型化的方式接收值。
How would I do this?我该怎么做?
I have tried this:我试过这个:
const { x: number, y: number } = await data.item(id);
But it will result in an error:但是会报错:
error TS2451: Cannot redeclare block-scoped variable 'number'.
How to do this?这该怎么做?
I'm not convinced your function's return type makes sense (what is an array combined with an object?) but regardless you shouldn't need to declare your types when destructuring the response.我不相信你的函数的返回类型有意义(什么是数组与对象的结合?)但无论如何你在解构响应时不需要声明你的类型。 TypeScript will infer that x and y are numbers based on the function signature
TypeScript 会根据函数签名推断 x 和 y 是数字
const { x, y } = await data.item(id);
Is sufficient to know that x and y are number
types足以知道 x 和 y 是
number
类型
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.