-
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
Idea: writeable(instance) #955
Comments
I'd personnaly vote for the IDEA 3 |
@k-g-a from IDEA 3 would you prefer option A, B or C? (I just named them and added a new one) |
From initial two options I'd prefer generic's one, but Thrid optins looks attractive. We could call it |
sadly I think I also like IDEA 3-C the most, but I'm scared that |
Great stuff @xaviergonz! I like Idea 3, option C is the best as well I agree, |
should I create a PR then? |
YESSS!
Op vr 10 aug. 2018 23:55 schreef Javier Gonzalez <[email protected]>:
… should I create a PR then?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#955 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABvGhKAN28IA1B2idLkl7WPaIeJSM8G_ks5uPgFUgaJpZM4Vo1zG>
.
|
@mweststrate @k-g-a // maybe CreationOrInstance?
type SnapshotOrInstance<T> = T extends IStateTreeNode<infer STNC, any> ? STNC | T : T extends IType<infer TC, any, infer TT> ? TC | TT : T;
type CastedType<T> = T extends IStateTreeNode<infer STNC> ? STNC | T : T;
function cast<T = never>(val: CastedType<T>): T {
return val as any as T;
}
const NumberArray = types.array(types.number)
const NumberMap = types.map(types.number)
const A = types.model({ n: 123, n2: types.number, arr: NumberArray, map: NumberMap }).actions((self) => ({
// for primitives (although not needed)
setN(nn: SnapshotOrInstance<typeof self.n>) {
self.n = cast(nn);
},
setN2(nn: SnapshotOrInstance<typeof types.number>) {
self.n = cast(nn);
},
setN3(nn: SnapshotOrInstance<number>) {
self.n = cast(nn);
},
setN4(nn: number) {
self.n = cast(nn)
},
setN5() {
self.n = cast(5)
},
// for arrays
setArr(nn: SnapshotOrInstance<typeof self.arr>) {
self.arr = cast(nn);
},
setArr2(nn: SnapshotOrInstance<typeof NumberArray>) {
self.arr = cast(nn)
},
setArr3(nn: number[]) {
self.arr = cast(nn)
},
setArr4() {
// it works even without specifying the target type, magic!
self.arr = cast([2,3,4])
self.arr = cast(NumberArray.create([2,3,4]))
},
// for maps
setMap(nn: SnapshotOrInstance<typeof self.map>) {
self.map = cast(nn);
},
setMap2(nn: SnapshotOrInstance<typeof NumberMap>) {
self.map = cast(nn)
},
setMap3(nn: {[k: string]: number}) {
self.map = cast(nn)
},
setMap4() {
// it works even without specifying the target type, magic!
self.map = cast({a: 2, b: 3})
self.map = cast(NumberMap.create({a: 2, b:3}))
}
}))
const C = types.model({ a: A}).actions((self) => ({
// for submodels, using typeof self.var
setA(na: SnapshotOrInstance<typeof self.a>) {
self.a = cast(na);
},
// for submodels, using the type directly
setA2(na: SnapshotOrInstance<typeof A>) {
self.a = cast(na);
},
// works too
setA3(na: typeof A.CreationType) {
self.a = cast(na);
},
// works too
setA4(na: typeof A.Type) {
self.a = cast(na);
},
setA5() {
// it works even without specifying the target type, magic!
self.a = cast({n2: 5})
self.a = cast(A.create({ n2: 5 }))
}
}))
const c = C.create({a: {n2: 5}})
// all below works
c.setA({ n2: 5 })
c.setA(A.create({ n2: 5 }))
c.setA2({ n2: 5 })
c.setA2(A.create({ n2: 5 }))
c.setA3({ n2: 5 })
// c.setA3(A.create({ n2: 5 })) // this one doesn't work, but that's expected, it wants the creation type
// c.setA4({n2: 5}) // this one doesn't work, but that's expected, it wants the instance type
c.setA4(A.create({ n2: 5 }))
c.setA5()
c.a.setN(1);
c.a.setN2(1)
c.a.setN3(1)
c.a.setN4(1)
c.a.setN5()
c.a.setArr([])
c.a.setArr(NumberArray.create([]))
c.a.setArr2([])
c.a.setArr2(NumberArray.create([]))
c.a.setArr3([])
c.a.setArr3(NumberArray.create([]))
c.a.setArr4()
c.a.setMap({a:2, b:3})
c.a.setMap(NumberMap.create({ a: 2, b: 3 }))
c.a.setMap2({ a: 2, b: 3 })
c.a.setMap2(NumberMap.create({ a: 2, b: 3 }))
c.a.setMap3({ a: 2, b: 3 })
// c.a.setMap3(NumberMap.create({ a: 2, b: 3 })) // doesn't work as expected, wants a plain object
c.a.setMap4()
const arr = types.array(A).create();
arr[0] = cast({n2: 5})
const map = types.map(A).create();
map.set('a', cast({n2: 5})) // not really needed in this case, but whatever :)
// and the best part, it actually doesn't work outside assignments :DDDD
// all this fails to compile
// cast([])
// cast({a:5})
// cast(NumberArray.create([]))
// cast(A.create({n2: 5}))
// cast({a: 2, b: 5})
// cast(NumberMap({a: 2, b: 3})) the good thing is that you can actually use the type of the variables themselves, which is helpful for cases like with idea 3 you are forced to separate them like
or do some repetition
with this new idea you don't have to if you don't want to
to be honest I don't even know how setA5/setArr4 even works, but it does! must be some TS inference magic not to mention the best part, that **the cast function (without a given type template) doesn't even work outside an assignment 💃 ** |
updated the sample with arrays and maps |
next questions (if IDEA 4 is liked more, for sure I do)
|
to put it into perspective against the other options IDEA 4
magic! |
just made a PR for idea 4 |
Released, closing |
I have:
Not following the above template might result in your issue being closed without further notice
wild idea,
given
as you know this fails:
given that TS does not accept different types for getting/setting values on models/arrays
so, how about something like this?
IDEA 1
Requirements:
IDEA 2
personally I don't like it as much as IDEA 1 though
Requirements:
IDEA 3
a dummy provided function that given type will ensure that the input is a proper snapshot and "output" the instance type
in reality the function won't do anything
Requirements:
basically it would be as create, but with the improvement that it doesn't require to specify an environment, since it will just inherit the one from the parent
The text was updated successfully, but these errors were encountered: