-
Notifications
You must be signed in to change notification settings - Fork 642
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Typescript error when using Array.prototype.filter or spread-operator #957
Comments
That's because sadly TS doesn't support different types for property getters and property setters Sadly, to overcome this you have to either cast it to the instance type (since internally mst will convert the snapshot to an instance) or create the instance directly. const Platform = types.model("Platform", {
name: types.string
})
const PlatformArray = types.array(Platform)
export const PlatformStore = types
.model("PlatformStore", {
platforms: PlatformArray
})
.actions(self => ({
add: (name: string) => {
// choose one
self.platforms = [...self.platforms, { name }] as typeof PlatformArray.Type
self.platforms = PlatformArray.create([...self.platforms, { name }])
},
remove: (name: string) => {
// choose one
self.platforms = self.platforms.filter(platform => platform.name !== name) as typeof PlatformArray.Type
self.platforms = PlatformArray.create(self.platforms.filter(platform => platform.name !== name))
}
})) The proposal I wrote in #955 is to overcome this issue. If you have further ideas on how to improve this please feel free to contribute there 👍 |
If it helps... Current solution is to use + import {cast} from 'mobx-state-tree'
const Platform = types.model('Platform', {
name: types.string
});
export const PlatformStore = types
.model('PlatformStore', {
platforms: types.array(Platform)
})
.actions(self => ({
add: (name: string) => {
- // error
- self.platforms = [...self.platforms, { name }];
+ self.platforms = cast([...self.platforms, { name }]);
},
remove: (name: string) => {
// error
self.platforms = self.platforms.filter(
platform => platform.name !== name
);
}
}));
|
Just ran into another gotcha when trying to filter an array which contained a model with a map of a union. Sample below. Not sure if this is a bug or not, but there is a workaround. See below. Attemp #1 and #2 are to illustrate where I was hitting the roadblocks. Possibly related - #913 import {cast, types} from 'mobx-state-tree'
const Swing = types.model({})
const Slide = types.model({})
const Playground = types.model({
id: types.identifier,
items: types.map(types.union(Swing, Slide))
})
const Store = types.model({
playgrounds: types.array(Playground)
})
.actions(self => ({
filterItems() {
// attempt #1
self.playgrounds = self.playgrounds.filter(item => item.id !== 'something')
// attempt #2
self.playgrounds = cast(self.playgrounds.filter(item => item.id !== 'something'))
}
})) Attempt #1Typescript complains that it's not no longer an observable array
Attempt #2Oh no! We got an
SolutionWrap the map in a - items: types.map(types.union(Swing, Slide))
+ items: types.maybe(types.map(types.union(Swing, Slide)))
- // Attempt #1
- self.playgrounds = self.playgrounds.filter(item => item.id !== 'something')
// Attempt #2
self.playgrounds = cast(self.playgrounds.filter(item => item.id !== 'something')) |
I am using mobx-state-tree 3.02 and mobx 5.0.3
typescript 3.0.1
when filtering or reassigning an array via spread, I get a typescript error:
due to the complexity of the typings I cannot read anything from this.
The text was updated successfully, but these errors were encountered: