-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Using Immutable.js’s Maps with TypeScript #683
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
Comments
I'm currently following advice from #341 (comment) but this involves overwriting the type definitions. I would also prefer a way to do this without requiring defaults to be set—ideally TypeScript should throw an error if you tried to create an object missing a property. |
To be honest I spent a few days now and a month ago trying to find a solution that would fit all the points including a record inheritance with custom methods, attributes checking and properties accessing using a dot-notation, but I still haven't found it. This feature mixed with a record generic type definitions you mentioned above could solve most of them microsoft/TypeScript#2225. But there is no progress. @leebyron Could you help us? |
One suggestion is to use Immutable’s Records, but in this case I don't want to allow default values. |
@OliverJAsh if records work for you setting |
Yes, correct, it's just boilerplate really. On Tue, 28 Jun 2016 at 19:54 Eugene Mirotin notifications@github.com
|
I found a class to get prop name as string array, then implemented a SetValue Method this has some rough edges. export class NavigableObject<T>{
constructor(private obj: T, private path: string[] = []) { }
To<R>(p: (x: T) => R): NavigableObject<R> {
let propName = this.getPropName(p)
if (propName) {
return new NavigableObject<R>(
p(this.obj),
this.path.concat(propName)
);
} else {
return new NavigableObject<R>(
p(this.obj),
this.path
);
}
}
getPath() {
return this.path;
}
private static propertyRegEx = /\.([^\.;]+);?\s*\}$/;
private getPropName(propertyFunction: Function) {
let value = NavigableObject.propertyRegEx.exec(propertyFunction.toString())
if (value)
return value[1];
}
}
function NavigatableRecordFactory<X>(defaultValues: X, name?: string) {
abstract class NavigatableRecord<P extends NavigatableRecord<P>> extends Record(defaultValues, name) {
SetValue<T>(fn: (x: NavigableObject<P>) => NavigableObject<T>, value: T) {
return this.setIn(fn(new NavigableObject<any>(this)).getPath(), value)
}
}
return NavigatableRecord;
}
interface IUSER {
Name: string;
Age: number;
}
export class USER extends NavigatableRecordFactory<IUSER>({
Name: "Simy the bothless",
Age: 27,
})<USER> implements IUSER {
Name: string;
Age: number;
} and then use it like state.Name // works
state.SetValue(t => t.To(q => q.Name), "test string") // typecheks
state.SetValue(t => t.To(q => q.Name), 123) // error it also works with nested properties somenestedImmutable.SetValue(t =>t.To(q => q.Date).To(q => q.Time), 213213123) but cant get it work without needing to implement method in child class , |
Hi Guys, regards Sean Checkout the Ultimate Angular 2 Boorstrap App: @ http://ng2.javascriptninja.io |
Fixed in master, will be released soon |
looking fwd to it... |
was this released already? |
fyi I tried with latest release immutable beta rc and no luck
|
@leebyron Can we see any example of usage? |
ya same... really need an example please |
Is this released? An example would be great! |
any news on this? |
@leebyron can you please post the commits related to fix? |
Hello from july 6 |
Hello from July 26th |
@leebyron any news on this? would love to be able to Type Maps |
@leebyron Hello from October 2017! an example would be much appreciated :) |
same! |
You may want to look to the TypeScript documentation for examples of how to use TypeScript. Map is defined in terms of the type of the keys it contains and the type of the values it contains, respectively. It appears as: For example, if you wanted a Map with string keys and number values, you'd write For cases where the type of value at each key will be different depending on the key, then You can see examples of using Record in the Immutable.js documentation here: http://facebook.github.io/immutable-js/docs/#/Record |
An example usage for those wondering (following this post), where you want to type check every property accesed via
What you do is first to declare a typing interface for your
Thus you'll have type checking over the getter functions of |
That's great, can't wait to test it |
Hi there, I make some attempt and get a solution with out any like this: interface ImMap<T, K, V> extends Map<K, V> {
toJS(): T;
get<I extends keyof T>(key: I & K): T[I] & V;
set<S extends keyof T>(key: S & K, value: T[S] & V): Map<K, V>;
}
//test
interface StringMap {
[propName: string]: string;
}
type ImStringMap = ImMap<D.StringMap, string, string>;
interface Attach {
val: string;
type: number;
sh1?: string;
keyName?: string;
}
type ImAttach = ImMap<D.Attach, string, string | number>; It works well for me. |
@odiseo42 @Saul-Mirone thanks for the examples! |
seems to work:
and got auto completion and error detection in Angular 5 / TS |
Can anyone help answer this question: https://stackoverflow.com/questions/52824312/how-to-use-immutablejs-map-with-typescript I can't figure out how to define my ImmutableMap in TypeScript that contains a List of (child) ImmutableMaps. |
@odiseo42 What if I need to access the full object. Any workaround for that? |
@kushalmahajan this project more dead than alive, try immer instead |
@JustFly1984 Thanks for the suggestion but I cannot just try new package. Already deep into it. |
Well, I was deep in immutable.js with 5 projects,until we had to refactor all the projects to typescript, it happened that there is no support :/ we had to refactor anyway, and used immer as 100% typescript support. Also this library is unmaintained for way too long and there is some bugs which killing all the benefits. For example if you have a list of strings more than 32 items, it will iterate without an order :/ |
Guys in 2020 and with ES6 you don't need immutable, IMHO :) |
@DonikaV Immutability by itself can be provided with Object.freeze() recursively or shallow. Or simply use |
I'm using immutable.js with TypeScript. I want a typed immutable object, e.g. an object called
Person
with an interface of:(name: string, age: number)
.Ideally I could construct a
Person
and it will be an instance ofImmutable.Map
:Is this possible? Sorry if my terminology is not entirely clear!
Thanks 😄
The text was updated successfully, but these errors were encountered: