Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript type casting when destructuring [duplicate]

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.

like image 836
Alkis Kalogeris Avatar asked Nov 26 '16 15:11

Alkis Kalogeris


1 Answers

If Typescript can not deduce the type of the result of groupBy you could try asserting it yourself.

function groupBy(o: any) {
    return o; // return any
}

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

// we know better than tsc and assert the type
let {a, b} = <{ a: number, b: string }>groupBy(x);
like image 82
toskv Avatar answered Oct 23 '22 17:10

toskv