Comments (4)
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.
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.
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.
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)
- How to Limit Undo Levels with undoMiddleware in mobx-keystone? HOT 1
- Tooling, devtools support HOT 4
- Supporting something like array.insert? HOT 2
- Unexpected value of context from detached node HOT 2
- Stream utilities on top of keystone HOT 3
- Mental model and idea behind keystone's tweaks/patches related to mobx observe
- Root Registry / Backrefs with deep observation HOT 5
- Keystone types supporting `onActionMiddleware` HOT 3
- Is it valid to start actions in onActionMiddleware's onStart/onFinish? HOT 2
- How to listen "initial" onPatches on RootStore?
- incorrect detaches from root store when applying snapshot HOT 4
- model does not detached from root store if it in array HOT 4
- Exception when detaching model that have active reaction with reference HOT 4
- Not able to access static methods of parent class from child class when inheriting with generic types. HOT 1
- Does it support Typescript 5.X features? HOT 3
- Unnecessary loops causes low performance, how to optimize the performance HOT 2
- Binary data in models HOT 3
- getRootStore returns undefined. HOT 5
- yjs Map not recommended for key-value data. HOT 4
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 mobx-keystone.