patterntypescriptCritical
Types in object destructuring
Viewed 0 times
destructuringobjecttypes
Problem
This
and this
will reasonably cause an error.
And this
will just destructure
How can types be specified for destructured object properties?
const { foo: IFoo[] } = bar;and this
const { foo: Array } = bar;will reasonably cause an error.
And this
const { foo: TFoo } = bar;will just destructure
TFoo property.How can types be specified for destructured object properties?
Solution
It turns out it's possible to specify the type after
Which in reality is not any better than plain old
: 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;Code Snippets
const {foo}: {foo: IFoo[]} = bar;const foo: IFoo[] = bar.foo;Context
Stack Overflow Q#39672807, score: 509
Revisions (0)
No revisions yet.