简体   繁体   English

Typescript - 使用数组参数中的元素作为返回类型

[英]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 在两个类型参数AB中是通用的。 (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])

Playground link to code Playground 代码链接

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

相关问题 如果数组的一个元素不为空而不使用Iterators - Typescript,则返回false - Return false if one element of array is not empty without use Iterators - Typescript 如何使用 typescript 返回一个导入数组类型的所有项之和? - How to use typescript to return the sum of all items of an imported array type? Typescript Generics:使用 output 参数类型来构建输入参数类型 - Typescript Generics: Use output argument type to build input argument type 如何将 typescript 类型添加到数组到 function 参数中? - How to add typescript type to an array into function argument? 什么打字稿类型用于函数参数? - What typescript type use for function argument? 使用参数的属性作为 typescript 中的类型 - Use argument's property as type in typescript Typescript,'NodeListOf<element> ' 不是数组类型或字符串类型</element> - Typescript,'NodeListOf<Element>' is not an array type or a string type TypeScript 参数的函数返回类型显示错误类型 - TypeScript function's return type from argument shows wrong type React 组件上的 Typescript 错误:“元素”类型的参数不可分配给类型的参数 - Typescript error on React Component: Argument of type 'Element' is not assignable to parameter of type typescript:根据回调返回类型验证函数参数 - typescript: validate function argument based on callback return type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM