Skip to content
Discussion options

You must be logged in to vote

This behavior is caused by TypeScript's useDefineForClassFields setting, which is enabled by default in Next.js (because it targets ES2022+).

What's happening:

When useDefineForClassFields: true, TypeScript compiles class fields using Object.defineProperty directly on the instance in the constructor:

// Your TypeScript
class MyClass {
  public myPublicField: string;
}

// Compiled JavaScript (with useDefineForClassFields: true)
class MyClass {
  constructor() {
    Object.defineProperty(this, "myPublicField", {
      enumerable: true,
      configurable: true,
      writable: true,
      value: void 0
    });
  }
}

This instance property shadows your prototype property, which is why defin…

Replies: 3 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by chrismiddle10
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@icyJoseph
Comment options

@chrismiddle10
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
3 participants