alkis alkis - 8 days ago 5
TypeScript Question

Typescript type casting when destructuring

Let's assume this

{foo, bar} = groupBy(arg);


I want to cast foo to type
Foo
and bar to type
Bar
. How can I do this?

I'm a complete beginner with Typescript.
groupBy
is the one from lodash package.

Answer

You can define the type of each variable in the deconstruction definition.

let x = { a: 1, b: "1" }

let {a: number, b: string} = x;
Comments