Next.js Discord

Discord Forum

NextJs compiler behaves differently than vanilla node - gnarly bugs

Unanswered
Yellow-throated Vireo posted this in #help-forum
Open in Discord
Avatar
Yellow-throated VireoOP
Hi all, when I run this code in the NextJs api, the result is different than when I run the script as compiled code via node

Code
  type ProductSnapshot = {
    name: string
    description: string
  }
  abstract class BaseValueObject {
    protected constructor(input: Prisma.JsonValue) {
      Object.assign(this, input)
      console.log({ me1: this, name1: (this as any)?.name ?? undefined })
    }
  }
  class ProductSnapshotEntity extends BaseValueObject implements ProductSnapshot {
    name!: string
    description!: string

    constructor(input: Prisma.JsonValue) {
      super(input)
      console.log({ me2: this, name2: this.name })
    }
  }
  const entity = new ProductSnapshotEntity({
    name: 'hello',
    description: 'hello',
  })
  console.log({ entity })

Result in NextJs
{
  me1: ProductSnapshotEntity { name: 'hello', description: 'hello' },
  name1: 'hello'
}
{
  me2: ProductSnapshotEntity { name: 'hello', description: 'hello' },
  name2: 'hello'
}
{
  entity: ProductSnapshotEntity { name: 'hello', description: 'hello' }
}

Result when I run code via Node
{
  me1: ProductSnapshotEntity { name: 'hello', description: 'hello' },
  name1: 'hello'
}
{
  me2: ProductSnapshotEntity { name: undefined, description: undefined },
  name2: undefined
}
{
  entity: ProductSnapshotEntity { name: undefined, description: undefined }
}

It's causing gnarly/hard-to-debug bugs that my backend code run in Next behaves differently than backend code run via other (backend-worker). Anyone have suggestions with how to set up Next (or my backend-worker compiler/tsconfig) so that the behavior is consistent? Thanks 🙏

1 Reply

Avatar
Yellow-throated VireoOP
Has to do with useDefineForClassFields