Comments (3)
I'm not sure if this is a feature or a bug: when I create a new instance of a dataclass through .create all class attributes have been made optional.
They are not "made optional". .create()
receives a subset of fields, assuming that omitted keys will be using default values.
class User extends Data { name: string; age: number; }
What version (or configuration) of TypeScript you're using? This code suppose to raise error of properties that have no initializer.
Prior to v2
, optional fields were not explicitly supported because information about this fields was missing in runtime. Starting from v2
you can define name?: string
which will provide string | undefined
type, just like the signature suggests.
from dataclass.
What if I don't want to assign default values but want to require certain values to be provided on initialization/creation?
I looked into your source code. They way you've TS-declared the create
method it makes all attributes optional here because you're using Partial
. However, the fix is not that simple because there is no way to distinguish class properties from class methods in TypeScript. So if we were to remove Partial
here the create
method would also require all data class methods which are not part of the constructor.
Basically, to address this issue we'd have to rewrite your whole implementation. So we can just document this issue as a known limitation and close the issue.
from dataclass.
Please read through the docs, most likely your use case is not covered by the library.
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
- Attemp to instantiate a class raises "TypeError: Cannot redefine property" on NodeJS 14.17.6 HOT 2
- 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.