Home > Blockchain >  Is there a way to force a type in Typescript?
Is there a way to force a type in Typescript?

Time:11-29

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[]'. ?

CodePudding user response:

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
  • Related