typescript - 如何在 .map 中定义解构参数的类型

标签 typescript

我在 map 中有一个解构的参数功能:

array.map(([name, data], index:number) => (
    console.log(name + data)
))
如何设置 namedataname: string , data: object ?

最佳答案

最好的方法是 array拥有正确的类型:

declare let array: Array<[string, object]>
array.map(([name, data], index:number) => (
    console.log(name + data)
))
您还可以将注释放在解构表达式上:
declare let array: Array<any>
array.map(([name, data]: [string, object], index:number) => (
    console.log(name + data)
))

关于typescript - 如何在 .map 中定义解构参数的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65809197/

相关文章:

来自嵌套对象的 TypeScript 模板文字字符串

node.js - NestJS MQTT 微服务的有效@MessagePattern 是什么?

reactjs - 如何使用包含 Action 的组件?出现错误 "Type ' { }' is not assignable to type ' IntrinsicAttributes"

javascript - typescript - 未定义命名空间问题

typescript - 如何在没有模块加载系统的情况下将我的 Typescript 编译成单个 JS 文件?

javascript - 把 http ://on hrefs missing a protocol in Angular

typescript - 为什么 typescript 在使用区分类型检查时无法推断返回类型

typescript - 遍历 Typescript Map

javascript - 获取 Angular 5/TypeScript 中的类(接口(interface))属性而不分配默认值

typescript - 如何在 Typescript 中编写递归 NonNullable 类型?