Theya
Theya

Reputation: 73

Is there a way to force a type in Typescript?

I have this:

      const res: any = await table.find({}).toArray();
      const data: Type[] = res;

Is there a way to do something like this:

      const res: Type[] = await table.find({}).toArray();

without the error : Type 'Document[]' is not assignable to type 'Type[]'. ?

Upvotes: 1

Views: 2410

Answers (1)

Xeelley
Xeelley

Reputation: 1116

Check out Type Guards and Differentiating Types.

If you are sure that res always will be array of Type, you can force type using as:

const res = await table.find({}).toArray() as Type[]; // res: Type[] now

Upvotes: 4

Related Questions