TypeScript | Immutable | proper way of extending Immutable.Map type

后端 未结 2 1177
星月不相逢
星月不相逢 2021-01-03 05:21

I have a react-redux application written in typescript with immutable package. There I have a data, which comes from api and in store I pack it to Map. In all application th

2条回答
  •  忘掉有多难
    2021-01-03 05:49

    Leaving that here in case it helps someone:

    import { Map } from 'immutable';
    
    export interface ImmutableMap extends Omit, 'set' | 'get' | 'delete'> {
        set: (key: AKeyOfThatJsObject, value: ValueOf>) => ImmuMap;
        get: (key: AKeyOfThatJsObject) => ValueOf>;
        delete: (key: AKeyOfThatJsObject, value: ValueOf>) => ImmuMap;
    }
    

    It's really working great for us!

    We use it like so: const aMap: ImmutableMap

    Example Usage:

    type BarType = { bar: string };
    
    const foo: BarType = {
        bar: 'abc';
    };
    const immutableBar: Immutable = Map(foo);
    // immutableBar.get suggests bar as the key, and knows that the result is a string.
    
    

提交回复
热议问题