Giter Site home page Giter Site logo

Comments (3)

alexeyraspopov avatar alexeyraspopov commented on July 23, 2024

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.

mstykow avatar mstykow commented on July 23, 2024

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.

alexeyraspopov avatar alexeyraspopov commented on July 23, 2024

Please read through the docs, most likely your use case is not covered by the library.

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.