Giter Site home page Giter Site logo

Comments (4)

xaviergonz avatar xaviergonz commented on June 9, 2024

I just released v1.6.0, which removes the restriction of not being able to redeclare a property on an extended model. Note that it is now on the user side to make sure the base and the extended properties are compatible (basically kept the same). If care is not taken the type of the property might become "never", be wrong, or it might fail in runtime.

example:

@model("MyApp/Point")
class Point extends Model({
  x: prop(1),
  y: prop(2),
}) {}

@model("MyApp/Point3d")
class Point3d extends ExtendedModel(Point, {
  y: prop(100), // this overrides the default value
  z: prop(3),
}) {}

does that address your issue?

from mobx-keystone.

feliksik avatar feliksik commented on June 9, 2024

Wow that's fast :-)
But I'm afraid is not working as I expected. E.g. if the parent has no defaults, but the subclass does have a default, the ModelCreationData type should become a bit looser; that is, I would expect to be able to omit y and z in the constructor data, as they have defaults; but Typescript does not like it:

@model("MyApp/Point")
class Point extends Model({
    x: prop<number>(),
    y: prop<number>(),
}) { }

@model("MyApp/Point3d")
class Point3d extends ExtendedModel(Point, {
    y: prop(100), // this overrides the default value
    z: prop(3),
}) { }

const p1: Point3d = new Point3d({
    x: 1, y: 2, z: 3 // ok 
})
const p2: Point3d = new Point3d({
    x: 1 // typescript complains that this does not fit the { x: number; y: number; } type of `Point`
})

Typescript says:

Argument of type '{ x: number; }' is not assignable to parameter of type '{ y?: number | null | undefined; z?: number | null | undefined; } & {} & { x?: number | undefined; y?: number | undefined; } & { x: number; y: number; }'.
  Property 'y' is missing in type '{ x: number; }' but required in type '{ x: number; y: number; }'.ts(2345)

BTW: thank you @xaviergonz , you have made a great tool!!!

from mobx-keystone.

xaviergonz avatar xaviergonz commented on June 9, 2024

I'm afraid changing the type itself when inheriting does not get along well with typescript :-/

I don't know if you share lots of logic, but you could alternatively do this (not saying it is better than your current method though, it is more like a mixing):

const props = {
  x: prop<number>(),
  y: prop<number>(),
} as const

function getSum2d(this: Point) {
  return this.x + this.y
}

@model("MyApp/Point")
class Point extends Model(props) {
  getSum2d = getSum2d
}

@model("MyApp/Point3d")
class Point3d extends Model({
  ...props,
  y: prop(100), // this overrides the default value
  z: prop(3),
}) {
  getSum2d = getSum2d
}

const p1: Point3d = new Point3d({
  x: 1,
  y: 2,
  z: 3, // ok
})
const p2: Point3d = new Point3d({
  x: 1, // ok
})

from mobx-keystone.

feliksik avatar feliksik commented on June 9, 2024

Aaah that's very interesting, thank you so much for your help Xavier! I'm rather new to the whole typescript ecosystem, so these kind of tricks did not occur to me right away.

I imagined there could be some super hacky magic with Omit on the parameters of ExtendedModel, but that's speculation, and in fact it's really not a big deal for me at the moment. Feel free to close this issue!

from mobx-keystone.

Related Issues (20)

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.