对象解构中的类型 [英] Types in object destructuring

查看:63
本文介绍了对象解构中的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

const { foo: IFoo[] } = bar;

还有这个

const { foo: Array<IFoo> } = bar;

将合理地导致错误.

还有这个

const { foo: TFoo } = bar;

只会破坏 TFoo 属性.

如何为变形对象属性指定类型?

How can types be specified for destructured object properties?

推荐答案

事实证明,可以为整个解构模式在:之后指定类型:

It turns out it's possible to specify the type after : for the whole destructuring pattern:

const {foo}: {foo: IFoo[]} = bar;

实际上,这并没有比普通的旧版本更好

Which in reality is not any better than plain old

const foo: IFoo[] = bar.foo;

这篇关于对象解构中的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆