- Notifications
You must be signed in to change notification settings - Fork638
-
Bug report
Sandbox link or minimal reproduction code Describe the expected behavior With the following input Describe the observed behavior
|
BetaWas this translation helpful?Give feedback.
All reactions
So the major issue here is thatIMSTMap<IT extends IAnyType> isalmost aMap<string | number, IT["Type"]>, but not quite because of this one method:
set(key: string | number, value: ExtractCSTWithSTN<IT>): thisIf we changevalue toIT["type"] you'll find TS infers the right thing forK andV. In its current form, I'm guessingV gets inferred asExtractCSTWithSTN<IT> | IT["Type"] which is the same asExtractCSTWithSTN<IT>.
That leads to the next problem: since this is a view,IT["CreationType"] obviously doesn't have that property (only the props!).
That sums up the problem. I'll think about if there's a solution or not, but I'm highly doubtful.
In the meantime, swap outMap<K, V> forIt…
Replies: 9 comments
-
Hey@evelant - thanks for putting together this issue. I'm sorry it's taken so long for us to get back to you. TypeScript inference is a known problem with MST, and it's definitely something we're trying to improve. That said, I don't have a great fix for you right now off the top of my head. If you found a solution to this, it would help a lot for others to see it. If you didn't and moved past it, no worries at all. For now, I'm going to mark this as a TypeScript issue, label it that help is welcome, and hopefully we can find a path forward to make this kind of thing easier. |
BetaWas this translation helpful?Give feedback.
All reactions
-
More info/perhaps related - TypeScript seems to have trouble inferring things about nested models in |
BetaWas this translation helpful?Give feedback.
All reactions
-
An example a little closer to the original problem:https://codesandbox.io/s/clever-margulis-ks8fx7?file=/src/App.tsx. |
BetaWas this translation helpful?Give feedback.
All reactions
-
Ooh, but |
BetaWas this translation helpful?Give feedback.
All reactions
-
I will check tomorrow |
BetaWas this translation helpful?Give feedback.
All reactions
-
I have looked into the map implementation, there are few places with |
BetaWas this translation helpful?Give feedback.
All reactions
-
Thanks@chakrihacker! We can keep this open and maybe revisit it over time. Lots of TypeScript work to be done, haha! |
BetaWas this translation helpful?Give feedback.
All reactions
👍 1
-
So the major issue here is that If we change That leads to the next problem: since this is a view, That sums up the problem. I'll think about if there's a solution or not, but I'm highly doubtful. In the meantime, swap out |
BetaWas this translation helpful?Give feedback.
All reactions
🎉 1
-
Thanks@thegedge - this is an excellent solution. Really appreciate your insight. I'm going to convert this issue to a discussion, which will close it. I forked the original reproduction and made the Here's the code for posterity: import"./styles.css";import{typesast}from"mobx-state-tree";/** * Like array.filter except on a map, returns value[] array of found results *@param m *@param predicate *@returns */exportfunctionfilterMapValues<K,V>(m:Iterable<[K,V]>,predicate:(v:V)=>any):V[]{constres:V[]=[];for(const[k,v]ofm){if(!!predicate(v)){res.push(v);}}returnres;}constfirstModel=t.model({}).views((self)=>({getfoo():string{return"foo";},}));constsecondModel=t.model({myMap:t.map(firstModel),});constm=secondModel.create({myMap:{test:{}}});//Views don't exist when extracting the value type of//an instance from a map in a generic function.//Instead we seem to get only the props.constf=filterMapValues(m.myMap,(val)=>val.foo==="foo");exportdefaultfunctionApp(){return<divclassName="App"></div>;} |
BetaWas this translation helpful?Give feedback.
All reactions
This discussion was converted from issue #1775 on February 26, 2024 01:01.