Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript stuck when compiling file that use immutable.js Map typing

I have angular2 app and i'm using immutable.js. When i use user Map function from immutable typescript compiler gets stuck. When i don't import the typing from immutable.js it works fine but i get errors for every function that i use from `immutable.js.

import {Map} from 'immutable';

this is the line that blocks typescript.

like image 433
Kliment Avatar asked Feb 09 '16 16:02

Kliment


2 Answers

The Map from immutable.js may conflict with TypeScript's internal Map. To resolve the collision, you can try something like import {Map} as MyMap from 'immutable' and use MyMap in your code.

You could also use import * as Immutable from 'immutable' and access features in the Immutable namespace.

like image 125
MatthewScarpino Avatar answered Nov 05 '22 11:11

MatthewScarpino


I have use immutable version 3.8.2 and it 's woring correcly with typescript.

import {Map} from 'immutable';

const map1 = Map( {a: 1, b: 4, c: 3 })
const map2 = map1.set('b', 2)

stackblitz typescript demo

Import map function like this import {Map} from 'immutable'; will conflict with javascript ES2015 Map so you cant use both of theme to solve this you can use another variable or alias to hold immutable map function like this

import {Map as _map} from 'immutable';

const map1 = _map( {a: 1, b: 4, c: 3 })
const map2 = map1.set('b', 2)

you can import entire immutable module mean all immutable function like this

import * as immutable from 'immutable';

const map1 = immutable.Map( {a: 1, b: 4, c: 3 })
const map2 = map1.set('b', 2)
like image 37
Muhammed Albarmavi Avatar answered Nov 05 '22 12:11

Muhammed Albarmavi