Comments (2)
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.
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)
- [Error] Class constructor Record cannot be invoked without 'new' HOT 5
- Object prototype undefinded HOT 7
- Value validation HOT 3
- Nested dataclasses are either not supported or undocumented HOT 1
- Document parameter properties as a partial alternative HOT 1
- `create` makes all properties optional HOT 3
- Dataclass doesn't work in Jupyter notebook HOT 3
- TypeError: Class constructor Data cannot be invoked without 'new' HOT 3
- Create data class without default values HOT 2
- How to use function return value as a field efault value HOT 1
- copy() method makes plain Object (TypeScript) HOT 1
- can you publish a new version? HOT 2
- Support for embedded class or object HOT 5
- Formal integration with yarn HOT 2
- Is it possible to define compulsory fields (without defaults)? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dataclass.