tag:blogger.com,1999:blog-1951542926024108564.post5635504775799805516..comments2024-01-12T19:52:27.760+11:00Comments on The Map Guy(de): Making Immutable.JS objects easier to work with in TypeScriptJackie Nghttp://www.blogger.com/profile/14319639645667992869noreply@blogger.comBlogger6125tag:blogger.com,1999:blog-1951542926024108564.post-91011431487659584012017-01-31T08:39:04.222+11:002017-01-31T08:39:04.222+11:00I created a TypeScript library to deal with this.
...I created a TypeScript library to deal with this.<br /><br />It also does stuctural-sharing, and provides full static analysis and completion, by using accessor functions.<br /><br />It also does not wrap your objects, simply provides functions to manipulate your states.<br /><br />It works very well with Redux, and we use it in all our projects.<br /><br />https://github.com/kube/monoliteAnonymoushttps://www.blogger.com/profile/01836030511571263021noreply@blogger.comtag:blogger.com,1999:blog-1951542926024108564.post-16557290191356228682016-12-31T06:37:35.627+11:002016-12-31T06:37:35.627+11:00You may also check out https://github.com/enginefo...You may also check out https://github.com/engineforce/ImmutableAssign, which is a lightweight immutable helper that supports full TypeScript type checking, and allows you to continue working with POJO (Plain Old JavaScript Object).<br /><br />It supports optional [deep-freeze](https://www.npmjs.com/package/deep-freeze) to freeze input and output, which can be used in development to make sure they don't change unintentionally by us or the 3rd party libraries.<br /><br />E.g.,<br /><br />```<br />var iassign = require("immutable-assign");<br />var _ = require("lodash");<br /> <br />var nested1 = { a: { b: { c: [1, 2, 3] } } };<br /> <br /> <br />// 4.1: Calling iassign() and _.map() to increment to every item in "c" array <br />var nested2 = iassign(<br /> nested1,<br /> function (n) { return n.a.b.c; },<br /> function (c) {<br /> return _.map(c, function (i) { return i + 1; });<br /> }<br />);<br />```engineforcehttps://www.blogger.com/profile/10632814045454982412noreply@blogger.comtag:blogger.com,1999:blog-1951542926024108564.post-77889886813159809192016-05-10T21:45:39.958+10:002016-05-10T21:45:39.958+10:00Re: Does this work for set() ?
Sadly I think not ...Re: Does this work for set() ?<br /><br />Sadly I think not (at least not as clean as get()). Your "foot in the door" set() method needs a known type for the value part, and if you're expecting different data types for various keys, then that means you almost always have to use either:<br /><br />set(key: "foo" | "bar" | "baz", value: any): IYourImmutableObjectType;<br /><br />or <br /><br />set< T >(key: "foo" | "bar" | "baz", value: T): IYourImmutableObjectType;<br /><br />But that would nullify any type safety on the value parameter for any specialized overloads of set() for "foo", "bar" and "baz" as you can pass in anything for the value as it will always resolve to the "foot in the door" set() method by the TS compiler should it not match the value parameter type in your specialized overload.<br /><br />Still, at least you're still constrained to a certain set of keys for set(), so passing any key not "foo", "bar" or "baz" will be a TS error so not all is lost, but it's not as rosy as the get() case.Jackie Nghttps://www.blogger.com/profile/14319639645667992869noreply@blogger.comtag:blogger.com,1999:blog-1951542926024108564.post-25559909423325342872016-05-10T21:13:56.269+10:002016-05-10T21:13:56.269+10:00How would you use booleans.How would you use booleans.Anonymoushttps://www.blogger.com/profile/06838125194165006077noreply@blogger.comtag:blogger.com,1999:blog-1951542926024108564.post-5109331369263815772016-05-10T21:05:52.312+10:002016-05-10T21:05:52.312+10:00Very nice. How would you do set for the map
map.s...Very nice. How would you do set for the map<br /><br />map.set('stringkey' 'stringVal' ) ??Anonymoushttps://www.blogger.com/profile/06838125194165006077noreply@blogger.comtag:blogger.com,1999:blog-1951542926024108564.post-51783924792661460832016-05-10T21:04:43.187+10:002016-05-10T21:04:43.187+10:00Very nice. But somewhat incomplate as I don't ...Very nice. But somewhat incomplate as I don't see anything about setting a value.<br /><br />How would you do set for the map<br /><br />map.set('stringkey' 'stringVal' ) ??<br /><br />ThanksAnonymoushttps://www.blogger.com/profile/06838125194165006077noreply@blogger.com