简体   繁体   English

如何在 typescript 中使用多种类型的数组调用 map function

[英]How to call map function with array of multiple types in typescript

I'm developing a Typescript function which needs to iterate over an array of type Employee[] | Guest[]我正在开发一个 Typescript function 需要遍历Employee[] | Guest[] Employee[] | Guest[] . Employee[] | Guest[] The problem is that TypeScript is emitting the error TS2349: This expression is not callable... has signatures but none of those are compatible with each other .问题是 TypeScript 发出错误TS2349: This expression is not callable... has signatures but none of those are compatible with each other I was expecting to be able to iterate the array and would just need to use a type guard before using the local variable, but that doesn't appear to be the case.我期望能够迭代数组,并且只需要在使用局部变量之前使用类型保护,但事实并非如此。 Is there a way I can use the map function without having to create a custom type?有没有一种方法可以使用map function 而无需创建自定义类型? The function will only ever need to iterate over the two types, so I was hoping to keep the parameter's type definition as Employee[] | Guest[] function 只需要遍历这两种类型,所以我希望将参数的类型定义保持为Employee[] | Guest[] Employee[] | Guest[] . Employee[] | Guest[] Below is a simple example where there's an error with people.map .下面是一个简单的示例,其中people.map存在错误。

interface Employee {
  id: string;
  firstName: string;
}

interface Guest {
  firstName: string;
}

function getFirstNames(people: Employee[] | Guest[]): string[] {
  return people.map((x: Employee | Guest) => {
    return x.firstName;
  });
}

const employeeFirstNames = getFirstNames([
  {id: '1', firstName: 'Steve'},
  {id: '2', firstName: 'James'},
] as Employee[]);

const guestFirstNames = getFirstNames([
  {firstName: 'Mary'},
] as Guest[]);

Type the array as union of Employee and Guest .将数组键入为EmployeeGuest的并集。

function getFirstNames(people: (Employee | Guest)[]): string[] {
  return people.map((x: Employee | Guest) => x.firstName);
}

I think you can use overload in this case, if you want to pass array with fixed element type我认为你可以在这种情况下使用重载,如果你想传递固定元素类型的数组

function getFirstNames(people: Employee[]): string[];
function getFirstNames(people: Guest[]): string[];
function getFirstNames(people: (Employee | Guest)[]): string[] {
  return people.map((x: Employee | Guest) => {
    return x.firstName;
  });
}

In this case, you won't be worried about public interface, so something like the following with mixed type will give error在这种情况下,你不会担心公共接口,所以像下面这样的混合类型会报错

const guestFirstNames = getFirstNames([
   { id: '1', firstName: 'Steve' },
  { firstName: 'Mary' }
]); // <-- No overload matches this call. Overload 1 of 2, '(people: Employee[]): string[]', gave the following error. Property 'id' is missing in type '{ firstName: string; }' but required in type 'Employee'. Overload 2 of 2, '(people: Guest[]): string[]', gave the following error. Type '{ id: string; firstName: string; }' is not assignable to type 'Guest'. Object literal may only specify known properties, and 'id' does not exist in type 'Guest'.

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

相关问题 如何在 TypeScript 中映射多种类型的数组? - How to map over array of multiple types in TypeScript? 如何在 Typescript 中映射函数的两个参数的类型? - How to map types of two arguments of function in Typescript? 如何将类型数组映射到 Typescript 中的另一个类型数组? - How can I map an array of types to another array of types in Typescript? 如何让 Typescript 从通用类型列表中的 Array.map 调用推断特定类型? - How to get Typescript to infer a specific type from an Array.map call from generic list of types? 打字稿:将对象从值映射到函数调用结果的高级类型 - Typescript: advanced types to map object from values to function call results Typescript:如何通过高级类型将 map 类型数组区分为不同的 arguments? - Typescript: how to map array of types to distinct arguments via advanced types? Typescript - 如何创建具有多种类型的数组 - Typescript - How to to create array with multiple types 使用 map 函数在一个动态类型问题中打字稿多种类型 - Typescript multiple types in one dynamic type issue with map function 与Typescript反应:带箭头功能的array.map()类型 - React with Typescript: Types for array.map() with arrow function 如何在Typescript中管理多个函数返回类型? - How to manage multiple function return types in Typescript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM