I'm wondering how you can assert the type of multiple variables when using destructuring in TypeScript.
Consider:
const { OneTypeOfObject, AndAnother } = ProtoBuf.loadProtoFile('./ohYesProtobufs.proto').build('iAmANameSpace');
How can I assert that OneTypeOfObject has a type of OneType and AndAnother has a type of OtherType?
I can make them all any like this:
const { OneTypeOfObject, AndAnother } = <any>ProtoBuf.loadProtoFile('./ohYesProtobufs.proto').build('iAmANameSpace');
What I'd actually like to do is something like this:
const { <OneType>OneTypeOfObject, <OtherType>AndAnother } = ProtoBuf.loadProtoFile('./ohYesProtobufs.proto').build('iAmANameSpace');
There's clearly been thought about this sort of thing:
https://github.com/Microsoft/TypeScript/commit/7a74d9f8d021f50201765d2170af8d249af90320
But I can't find the answer based on what I've seen....
Update
I tried both of David's approaches but neither worked. This is the code used for approach 2:
interface OneType extends ProtoBuf.ProtoBuf {
new(): any;
}
interface OtherType extends ProtoBuf.ProtoBuf {
new(): any;
}
const { <OneType>OneTypeOfObject, <OtherType>AndAnother } = ProtoBuf.loadProtoFile('./ohYesProtobufs.proto').build('iAmANameSpace');
It caused this error:
Type 'ProtoBuf' is not assignable to type '{ OneTypeOfObject: OneType; AndAnother: OtherType; }'. Property 'OneTypeOfObject' is missing in type 'ProtoBuf'.