-
Bug report
Sandbox link or minimal reproduction code Describe the expected behavior
With the following input
Describe the observed behavior
|
Beta Was this translation helpful? Give feedback.
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. |
Beta Was this translation helpful? Give feedback.
-
More info/perhaps related - TypeScript seems to have trouble inferring things about nested models in But in this CodeSandbox, TS is happy with us for |
Beta Was this translation helpful? Give feedback.
-
An example a little closer to the original problem: https://codesandbox.io/s/clever-margulis-ks8fx7?file=/src/App.tsx. |
Beta Was this translation helpful? Give feedback.
-
Ooh, but |
Beta Was this translation helpful? Give feedback.
-
I will check tomorrow |
Beta Was this translation helpful? Give feedback.
-
I have looked into the map implementation, there are few places with |
Beta Was this translation helpful? Give feedback.
-
Thanks @chakrihacker! We can keep this open and maybe revisit it over time. Lots of TypeScript work to be done, haha! |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
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 { types as t } from "mobx-state-tree";
/**
* Like array.filter except on a map, returns value[] array of found results
* @param m
* @param predicate
* @returns
*/
export function filterMapValues<K, V>(
m: Iterable<[K, V]>,
predicate: (v: V) => any
): V[] {
const res: V[] = [];
for (const [k, v] of m) {
if (!!predicate(v)) {
res.push(v);
}
}
return res;
}
const firstModel = t.model({}).views((self) => ({
get foo(): string {
return "foo";
},
}));
const secondModel = t.model({
myMap: t.map(firstModel),
});
const m = 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.
const f = filterMapValues(m.myMap, (val) => val.foo === "foo");
export default function App() {
return <div className="App"></div>;
} |
Beta Was this translation helpful? Give feedback.
So the major issue here is that
IMSTMap<IT extends IAnyType>
is almost aMap<string | number, IT["Type"]>
, but not quite because of this one method:If we change
value
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 out
Map<K, V>
forIt…