[英]Typescript - Use element in array argument as return type
I have a simple function as follow:我有一个简单的 function 如下:
const transform = ([a, b]) => ({operation: a, ...b});
How can I type this function to make sure the return type is an object containing a key operation
with the type of the argument a and the property of b.如何键入此 function 以确保返回类型是 object 包含具有参数 a 类型和 b 属性的键
operation
。
I tried the following but without success:我尝试了以下但没有成功:
function transform<[A, B]>(param: [A, B]): { operation: A } & B {
return { operation: param[0], ...param[1] };
}
Any help about that?有什么帮助吗? Thanks!
谢谢!
You have what amounts to a typographical error;你有什么相当于一个印刷错误; instead of
transform<[A, B]>(...)
, you need to write transform<A, B>(...)
like this:而不是
transform<[A, B]>(...)
,您需要像这样编写transform<A, B>(...)
:
function transform<A, B>(param: [A, B]): { operation: A } & B {
// ----> ^^^^^^ ^ ^ <------------> ^ ^
// declaring type params using the type params
//
return { operation: param[0], ...param[1] };
}
The transform()
function is generic in two type parameters A
, and B
. transform()
function 在两个类型参数A
和B
中是通用的。 (You can think of these almost like regular function parameters, but instead of corresponding to arguments that callers pass in as values , they are arguments that callers pass in as types .) The syntax for declaring the list of type parameters a generic function takes is to put a comma-separated list of these parameters inside angle brackets, immediately before the list of regular parameters in parentheses. (You can think of these almost like regular function parameters, but instead of corresponding to arguments that callers pass in as values , they are arguments that callers pass in as types .) The syntax for declaring the list of type parameters a generic function takes is将这些参数的逗号分隔列表放在尖括号内,紧挨在括号中的常规参数列表之前。
It's only later in the function signature and implementation where you use the type parameters as types.只是在 function 签名和实现的后面,您使用类型参数作为类型。 So the
param
function parameter is annotated as type [A, B]
, a tuple type where the first element is of type A
and the second element is of type B
.因此
param
function 参数被注释为类型[A, B]
,这是一个元组类型,其中第一个元素是A
类型,第二个元素是B
类型。 The fact that you were going to use the type [A, B]
has nothing to do with the syntax to declare the type parameters.您将使用类型
[A, B]
的事实与声明类型参数的语法无关。
That's the answer to the question as asked.这就是所问问题的答案。
I could go a little further and suggest that a more direct translation of your original transform
JavaScript code into TypeScript would look like this:我可以 go 更进一步,并建议将您的原始
transform
JavaScript 代码更直接地转换为 TypeScript 如下所示:
const transform = <A, B extends object>(
[a, b]: [A, B]
): { operation: A } & B => ({ operation: a, ...b });
Here you can see that even arrow functions can be generic, and therefore need a type parameter declaration before the parameter list (which must be in parentheses, even if your arrow function looks like x => x
... you can't write <T>x:T => x
, you need <T>(x: T) => x
).在这里您可以看到,即使箭头函数也可以是通用的,因此需要在参数列表之前进行类型参数声明(必须在括号中,即使您的箭头 function 看起来像
x => x
...您不能写<T>x:T => x
,你需要<T>(x: T) => x
)。 Also see that the return type annotation of an arrow function comes immediately after the parameter list with a colon, and before the arrow =>
.另请参阅箭头 function 的返回类型注释紧跟在带有冒号的参数列表之后,箭头
=>
之前。
Since you are going to use object spread on b
which of type B
, it's useful to tell the compiler that B
should be constrained to the object
type , so that nobody calls transform([1, 2])
and is possibly confused by what comes out.由于您将使用object 传播到
b
类型B
中,告诉编译器B
应该被限制为object
类型是有用的,这样就没有人调用transform([1, 2])
并且可能会被接下来的内容混淆出去。
Also, you can still use destructuring assignment in your function parameter, so we have ([a, b]: [A, B])
instead of (param: [A, B])
.此外,您仍然可以在 function 参数中使用解构赋值,因此我们使用
([a, b]: [A, B])
代替(param: [A, B])
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.