Skip to content

Commit

Permalink
try to fix and add set definition
Browse files Browse the repository at this point in the history
  • Loading branch information
jdeniau committed Jul 17, 2021
1 parent 720b847 commit c762557
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions __tests__/Map.ts
Expand Up @@ -82,6 +82,7 @@ describe('Map', () => {
it('accepts non-collection array-like objects as keyed collections', () => {
const m = Map({ length: 3, 1: 'one' });
expect(m.get('length')).toBe(3);
// @ts-expect-error -- type error, but the API is tolerante
expect(m.get('1')).toBe('one');
expect(m.toJS()).toEqual({ length: 3, 1: 'one' });
});
Expand Down
1 change: 1 addition & 0 deletions __tests__/getIn.ts
Expand Up @@ -75,6 +75,7 @@ describe('getIn', () => {
key: { regular: 'jsobj' },
list: List([Map({ num: 10 })]),
});
// @ts-expect-error -- expected error
expect(deep.getIn(['key', 'foo', 'item'])).toBe(undefined);
expect(deep.getIn(['key', 'foo', 'item'], 'notSet')).toBe('notSet');
expect(deep.getIn(['list', 0, 'num', 'badKey'])).toBe(undefined);
Expand Down
10 changes: 8 additions & 2 deletions type-definitions/Immutable.d.ts
Expand Up @@ -760,11 +760,12 @@ declare namespace Immutable {
* not altered.
*/
export function Map<K, V>(collection?: Iterable<[K, V]>): Map<K, V>;
export function Map<K extends string, V>(obj: { [P in K]?: V }): Map<K, V>;
export function Map<V>(collection: Iterable<List<V>>): Map<V, V>;
export function Map<R extends { [key in string | number]: unknown }>(
obj: R
): ObjectLikeMap<R>;
// export function Map<V>(obj: { [key: string]: V }): Map<string, V>;
export function Map<V>(obj: { [key: string]: V }): Map<string, V>;
export function Map<K extends string, V>(obj: { [P in K]?: V }): Map<K, V>;

export interface ObjectLikeMap<
R extends { [key in string | number]: unknown }
Expand Down Expand Up @@ -802,6 +803,11 @@ declare namespace Immutable {
>(
path: [K1, K2, K3, K4]
): GetMapType<GetMapType<GetMapType<R[K1]>[K2]>[K3]>[K4];

set<K extends string | number, V>(
key: K,
value: V
): this & ObjectLikeMap<{ [key in K]: V }>;
}

type GetMapType<S> = S extends ObjectLikeMap<infer T> ? T : S;
Expand Down
3 changes: 3 additions & 0 deletions type-definitions/ts-tests/map.ts
Expand Up @@ -115,6 +115,9 @@ import { Map, List } from 'immutable';

// $ExpectType Map<number, string | number>
Map<number, number | string>().set(0, 'a');

// // $ExpectType ObjectLikeMap<{ a: string; }>
// Map({ a: 1 }).set('b', 'b');
}

{
Expand Down

0 comments on commit c762557

Please sign in to comment.