[英]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
.将数组键入为
Employee
和Guest
的并集。
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.