Giter Site home page Giter Site logo

Comments (2)

alexeyraspopov avatar alexeyraspopov commented on August 25, 2024

There is an unfortunate thing happened some time ago that may potentially break the existing dataclass implementation completely. I'm still considering options and experimenting on the implementation level to make it work again.

Basically, what happened is the fact the proposal has advanced and Babel/browsers implementation of the proposal has followed. All the way back to the beginning of this project, "class property" was basically a shorthand syntax for assigning properties in constructor:

// source
class Example extends Record {
  prop = "value";
}

// what it means
class Example extends Record {
  constructor() {
    super()
    this.prop = "value";
  }
}

And this idea worked just great for dataclass.

However, moving forward, Babel/browser implementation started using Object.defineProperty():

class Example extends Record {
  constructor() {
    super();
    Object.defineProperty(this, 'prop', { value: "value", ... })
  }
}

Record's constructor includes all the magic and it is invoked first. If there was no special logic inside it, anything that Record assigns to this would be overrode by the following subclass constructor code, meaning all the props would always have "default" values and would ignore customs (new Example({ prop: ... })). That's the best case scenario. What happens in your case is "use strict" mode (which is default for ES Modules): attempting to use Object.defineProperty() on a prop that was already defined by Record's constructor throws a runtime error that you mentioned.

Most likely I'll need to publish a breaking change that would involve changing the DX: instead of new User({ name: "Liza" }) developers will need to do User.from({ name: "Liza" }) or User.create({ name: "Liza" }) or something similar. This will help avoid the limitation described above. This should also help fixing TypeScript generic issue (mentioned in README).

from dataclass.

alexeyraspopov avatar alexeyraspopov commented on August 25, 2024

To summarize, this issue has been tackled as a part of the new major release v2.0.0. It has breaking changes that I described above. The initial example now should look like this:

import { Data } from "dataclass";

class User extends Data {
  name = 'Anonymous';
}

let user = User.create({ name: 'Liza' });

I hope you will get a chance to try the new release, please let me know if you still facing any issues.

from dataclass.

Related Issues (16)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.