Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

192
Views
Error de mecanografiado: el tipo 'Documento []' no se puede asignar al tipo personalizado

Hay un servicio nestJS simple, que devuelve el resultado de una consulta mongoDb. Como puede ver, traté de establecer el tipo de resultado esperado, que es una matriz de documentos.

 class MyDataset { group: string location: string type: string reported: Date } async getDatasets(): Promise<Array<MyDataset>> { const Collection = this.db.collection('collection') const result = await Collection.find({}).toArray() return result // <-- ts error }

Pero me sale el error TS

 Type 'Document[]' is not assignable to type 'MyDataset[]'. Type 'Document' is missing the following properties from type 'MyDataset': group, location, type, reported

No veo, lo que estoy haciendo mal. ¿Alguien podría explicar el problema?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

El error implica una discrepancia entre el tipo que espera MyDataset y el que está devolviendo Collection.find({}).toArray() . La documentación de mongo db establece que los métodos toArray() devuelven una matriz de Documentos.

Esto replica el mismo comportamiento:

 type DocumentA = { group: string location: string type: string reported: Date } type DocumentB = { anotherField: string } const arrayOfDocumentsA = () => { const res: DocumentA[] = []; return res; } const arrayOfDocumentsB = () => { const res: DocumentB[] = []; return res; } const getDatasets = (): Array<DocumentA> => { return arrayOfDocumentsB(); // ts error };

Esto generará el siguiente error:

 Type 'DocumentB[]' is not assignable to type 'DocumentA[]'. Type 'DocumentB' is missing the following properties from type 'DocumentA': group, location, type, reported ts(2322)
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!