Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

136
Visualizações
Type 'Document<any>' is not assignable to type 'Pick<Pick<_LeanDocument<TicketDoc>

I have an error that is clear as mud on my terminal:

TSError: ⨯ Unable to compile TypeScript: [orders-depl-9fbcb84ff-ngt2z orders] src/models/ticket.ts(47,5): error TS2322: Type 'Document' is not assignable to type 'Pick<Pick<_LeanDocument, "_id" | "__v" | "id" | "title" | "price" | "isReserved">, "_id" | "__v" | "id" | "title" | "price"> | QuerySelector<...> | undefined'. [orders-depl-9fbcb84ff-ngt2z orders] Type 'Document' is missing the following properties from type 'Pick<Pick<_LeanDocument, "_id" | "__v" | "id" | "title" | "price" | "isReserved">, "_id" | "__v" | "id" | "title" | "price">': title, price

It's referencing this model file:

import mongoose from 'mongoose';
import { Order, OrderStatus } from './order';

interface TicketAttrs {
  title: string;
  price: number;
}

export interface TicketDoc extends mongoose.Document {
  title: string;
  price: number;
  isReserved(): Promise<boolean>;
}

interface TicketModel extends mongoose.Model<TicketDoc> {
  build(attrs: TicketAttrs): TicketDoc;
}

const ticketSchema = new mongoose.Schema({
  title: {
    type: String,
    required: true
  },
  price: {
    type: Number,
    required: true,
    min: 0
  }
}, {
  toJSON: {
    transform(doc, ret) {
      ret.id = ret._id;
      delete ret._id;
    }
  }
});

ticketSchema.statics.build = (attrs: TicketAttrs) => {
  return new Ticket(attrs);
};
// Run query to look at all orders. Find an order where the 
// ticket is the ticket just found *and* the order status is *not* cancelled.
// If we find an order from that means the ticket *is* reserved
ticketSchema.methods.isReserved = async function () {
  // this === the ticket document that I just called 'isReserved' on
  const existingOrder = await Order.findOne({
    ticket: this,
    status: {
      $in: [
        OrderStatus.Created,
        OrderStatus.AwaitingPayment,
        OrderStatus.Complete
      ]
    }
  });

  return !!existingOrder;
};

const Ticket = mongoose.model<TicketDoc, TicketModel>('Ticket', ticketSchema);

export { Ticket };

I don't see where I have made a syntax error anywhere and I have no idea what this type of Pick is. It seems like the issue is that ticket is not ticket but should be equal to this, except it's not.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Wow, this was one of those gotchas in TypeScript where you have to have deep knowledge to figure out. So here is how you make the error go away:

// Run query to look at all orders. Find an order where the 
// ticket is the ticket just found *and* the order status is *not* cancelled.
// If we find an order from that means the ticket *is* reserved
ticketSchema.methods.isReserved = async function () {
  // this === the ticket document that I just called 'isReserved' on
  const existingOrder = await Order.findOne({
    //@ts-ignore
    ticket: this,
    status: {
      $in: [
        OrderStatus.Created,
        OrderStatus.AwaitingPayment,
        OrderStatus.Complete
      ]
    }
  });

  return !!existingOrder;
};

I normally would not do this as it bypasses the TS checks that we rely on to make sure everything is type safe. For now though, it does the trick as the error was just bizarre since it was not technically anything I did wrong in developing the code, just a TypeScript quirk I guess.

over 4 years ago · Santiago Trujillo Relatório

0

I know it's been a long time. But, I saw this answer in user Jonathan's Udemy questions and answers and I think it's better than disabling TS verification:

Not a big fan of ts-ignore because of reasons stated by Korey above.

I suspect the cause of the problem is the extra isReserved() method added to a Ticket document.

Hence, instead of searching by the Ticket document (this), I searched via the id of the Ticket document (this.id). This should work fine.

ticketSchema.methods.isReserved = async function() {
  const existingOrder = await Order.findOne({
    ticket: this.id, // Ticket id
    status: {
      $in: [
        OrderStatus.Created,
        OrderStatus.AwaitingPayment,
        OrderStatus.Complete
      ]
    }
  })
  return !!existingOrder
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda