微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – Typescript:如何映射联合数组类型?

我有以下结构:

interface Test1 {
    number: number;
}
interface Test2 extends Test1 {
    text: string;
}

let test: Test1[] | Test2[] = [];
test.map(obj => {}); // does not work

我收到错误

Cannot invoke an expression whose type lacks a call signature. Type ‘{ (this: [Test1, Test1, Test1, Test1, Test1], callbackfn: (this: void, value: Test1, index: nu…’ has no compatible call signatures

如何映射测试变量?

解决方法:

问题是对于union类型,作为函数的成员也将被输入为union类型,因此map的类型将是(< U>(callbackfn

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐