8000 Add correct types for empty Seq and Collection (#1878) · immutable-js/immutable-js@d2522bd · GitHub
[go: up one dir, main page]

Skip to content

Commit d2522bd

Browse files
authored
Add correct types for empty Seq and Collection (#1878)
Fixes #1876
1 parent 611c559 commit d2522bd

File tree

3 files changed

+70
-10
lines changed

3 files changed

+70
-10
lines changed

type-definitions/immutable.d.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2882,7 +2882,9 @@ declare namespace Immutable {
28822882
* Note: `Seq.Indexed` is a conversion function and not a class, and does
28832883
* not use the `new` keyword during construction.
28842884
*/
2885-
function Indexed<T>(collection: Iterable<T> | ArrayLike<T>): Seq.Indexed<T>;
2885+
function Indexed<T>(
2886+
collection?: Iterable<T> | ArrayLike<T>
2887+
): Seq.Indexed<T>;
28862888

28872889
interface Indexed<T> extends Seq<number, T>, Collection.Indexed<T> {
28882890
/**
@@ -3044,7 +3046,7 @@ declare namespace Immutable {
30443046
* Note: `Seq.Set` is a conversion function and not a class, and does not
30453047
* use the `new` keyword during construction.
30463048
*/
3047-
function Set<T>(collection: Iterable<T> | ArrayLike<T>): Seq.Set<T>;
3049+
function Set<T>(collection?: Iterable<T> | ArrayLike<T>): Seq.Set<T>;
30483050

30493051
interface Set<T> extends Seq<T, T>, Collection.Set<T> {
30503052
/**
@@ -3148,7 +3150,7 @@ declare namespace Immutable {
31483150
collection: Collection.Indexed<T> | Iterable<T> | ArrayLike<T>
31493151
): Seq.Indexed<T>;
31503152
function Seq<V>(obj: { [key: string]: V }): Seq.Keyed<string, V>;
3151-
function Seq(): Seq<unknown, unknown>;
3153+
function Seq<K = unknown, V = unknown>(): Seq<K, V>;
31523154

31533155
interface Seq<K, V> extends Collection<K, V> {
31543156
/**
@@ -3325,7 +3327,7 @@ declare namespace Immutable {
33253327
* Note: `Collection.Keyed` is a conversion function and not a class, and
33263328
* does not use the `new` keyword during construction.
33273329
*/
3328-
function Keyed<K, V>(collection: Iterable<[K, V]>): Collection.Keyed<K, V>;
3330+
function Keyed<K, V>(collection?: Iterable<[K, V]>): Collection.Keyed<K, V>;
33293331
function Keyed<V>(obj: { [key: string]: V }): Collection.Keyed<string, V>;
33303332

33313333
interface Keyed<K, V> extends Collection<K, V> {
@@ -3495,7 +3497,7 @@ declare namespace Immutable {
34953497
* does not use the `new` keyword during construction.
34963498
*/
34973499
function Indexed<T>(
3498-
collection: Iterable<T> | ArrayLike<T>
3500+
collection?: Iterable<T> | ArrayLike<T>
34993501
): Collection.Indexed<T>;
35003502

35013503
interface Indexed<T> extends Collection<number, T> {
@@ -3793,7 +3795,7 @@ declare namespace Immutable {
37933795
* Note: `Collection.Set` is a factory function and not a class, and does
37943796
* not use the `new` keyword during construction.
37953797
*/
3796-
function Set<T>(collection: Iterable<T> | ArrayLike<T>): Collection.Set<T>;
3798+
function Set<T>(collection?: Iterable<T> | ArrayLike<T>): Collection.Set<T>;
37973799

37983800
interface Set<T> extends Collection<T, T> {
37993801
/**
@@ -3900,6 +3902,7 @@ declare namespace Immutable {
39003902
function Collection<V>(obj: {
39013903
[key: string]: V;
39023904
}): Collection.Keyed<string, V>;
3905+
function Collection<K = unknown, V = unknown>(): Collection<K, V>;
39033906

39043907
interface Collection<K, V> extends ValueObject {
39053908
// Value equality

type-definitions/ts-tests/empty.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { Seq, Collection } from 'immutable';
2+
3+
{
4+
// Typed empty seqs
5+
6+
// $ExpectType Seq<unknown, unknown>
7+
Seq();
8+
9+
// $ExpectType Seq<number, string>
10+
Seq<number, string>();
11+
12+
// $ExpectType Indexed<unknown>
13+
Seq.Indexed();
14+
15+
// $ExpectType Indexed<string>
16+
Seq.Indexed<string>();
17+
18+
// $ExpectType Keyed<unknown, unknown>
19+
Seq.Keyed();
20+
21+
// $ExpectType Keyed<number, string>
22+
Seq.Keyed<number, string>();
23+
24+
// $ExpectType Set<unknown>
25+
Seq.Set();
26+
27+
// $ExpectType Set<string>
28+
Seq.Set<string>();
29+
}
30+
31+
{
32+
// Typed empty collection
33+
34+
// $ExpectType Collection<unknown, unknown>
35+
Collection();
36+
37+
// $ExpectType Collection<number, string>
38+
Collection<number, string>();
39+
40+
// $ExpectType Indexed<unknown>
41+
Collection.Indexed();
42+
43+
// $ExpectType Indexed<string>
44+
Collection.Indexed<string>();
45+
46+
// $ExpectType Keyed<unknown, unknown>
47+
Collection.Keyed();
48+
49+
// $ExpectType Keyed<number, string>
50+
Collection.Keyed<number, string>();
51+
52+
// $ExpectType Set<unknown>
53+
Collection.Set();
54+
55+
// $ExpectType Set<string>
56+
Collection.Set<string>();
57+
}

type-definitions/ts-tests/exports.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ Seq; // $ExpectType typeof Seq
2525
Set; // $ExpectType typeof Set
2626
Stack; // $ExpectType typeof Stack
2727
Collection; // $ExpectType typeof Collection
28-
Collection.Set; // $ExpectType <T>(collection: Iterable<T> | ArrayLike<T>) => Set<T>
28+
Collection.Set; // $ExpectType <T>(collection?: Iterable<T> | ArrayLike<T> | undefined) => Set<T>
2929
Collection.Keyed; // $ ExpectType { <K, V>(collection: Iterable<[K, V]>): Keyed<K, V>; <V>(obj: { [key: string]: V; }): Keyed<string, V> }
30-
Collection.Indexed; // $ExpectType <T>(collection: Iterable<T> | ArrayLike<T>) => Indexed<T>
30+
Collection.Indexed; // $ExpectType <T>(collection?: Iterable<T> | ArrayLike<T> | undefined) => Indexed<T>
3131

3232
Immutable.List; // $ExpectType typeof List
3333
Immutable.Map; // $ExpectType typeof Map
@@ -40,6 +40,6 @@ Immutable.Seq; // $ExpectType typeof Seq
4040
Immutable.Set; // $ExpectType typeof Set
4141
Immutable.Stack; // $ExpectType typeof Stack
4242
Immutable.Collection; // $ExpectType typeof Collection
43-
Immutable.Collection.Set; // $ExpectType <T>(collection: Iterable<T> | ArrayLike<T>) => Set<T>
43+
Immutable.Collection.Set; // $ExpectType <T>(collection?: Iterable<T> | ArrayLike<T> | undefined) => Set<T>
4444
Immutable.Collection.Keyed; // $ ExpectType { <K, V>(collection: Iterable<[K, V]>): Keyed<K, V>; <V>(obj: { [key: string]: V; }): Keyed<string, V> }
45-
Immutable.Collection.Indexed; // $ExpectType <T>(collection: Iterable<T> | ArrayLike<T>) => Indexed<T>
45+
Immutable.Collection.Indexed; // $ExpectType <T>(collection?: Iterable<T> | ArrayLike<T> | undefined) => Indexed<T>

0 commit comments

Comments
 (0)
0