How to write a stream type definition for ImmutableJS types?

I see that ImmutableJS now has thread stream annotations , but how to determine the type? For instance:

const state : ??? = Immutable.fromJS({ name: 'chet', tags: ['something']})

I can determine the type from normal JS, but how can I say that it is Immutable.Map with specific keys?

+4
source share
3 answers

The problem is that immutable stream types support only one type definition for each combination of keys and values.

So unchanging. The cards accept Map<keyType, valueType>

and immutable.List accepts List<valueType>

Immutable.fromJS ({name: 'chet', tags: ['something']})

equivalent to Map ({name: 'chet', tags: List (['something])}

Map<(string) | ('name', 'tags'), string | List<string>>

+2

Record ( JS). , Record s, .

// @flow

import { Record, List } from 'immutable';
import type { RecordFactory, RecordOf } from 'immutable';

type StateProps = {
  name: string,
  tags: List<string>,
}

type State = RecordOf<StateProps>;

const makeState: RecordFactory<StateProps> = Record({
  name: '',
  tags: List(),
});

const state: State = makeState({
  name: 'chet',
  tags: List(['something']),
});

// Or, to to create an instance of the default
// const _state: State = makeState();
+2

I would write this type as

const state: Map<string, any>

This means that the state will be of type Map, and the map will have string keys and (name, tags), and the values ​​will be any.
Also note: you will have to do

import type { Map } from 'immutable';

otherwise it will read the map of the native type, and you will see errors such as Map does not have a get or getIn method.

+1
source

Source: https://habr.com/ru/post/1652530/


All Articles