the-dr-lazy / deox

Functional Type-safe Flux Standard Utilities
https://deox.js.org
MIT License
206 stars 12 forks source link

Strange return type of DeepImmutable and Immutable for ReadonlyArray and ReadonlyMap #65

Closed the-dr-lazy closed 5 years ago

the-dr-lazy commented 5 years ago

Summary

By nested DeepImmutable or directly passing ReadonlyArray and ReadonlyMap to DeepImmutable and Immutable types results in DeepImmutableObject<ReadonlyArray> and DeepImmutableObject<ReadonlyMap> which should be neutralized by the type itself.

Steps to reproduce

For ReadonlyArray:

type A = DeepImmutable<DeepImmutable<string[]>>

For ReadonlyMap:

type B = DeepImmutable<DeepImmutable<Map<'key', 'value'>>>

Expected result(s)

For ReadonlyArray:

type A = DeepImmutableArray<string>

For ReadonlyMap:

type B = DeepImmutableMap<'key', 'value'>

Actual result(s)

For ReadonlyArray:

type A = {
    readonly [x: number]: string;
    readonly length: number;
    readonly toString: () => string;
    readonly toLocaleString: () => string;
    readonly concat: {
        (...items: ConcatArray<string>[]): string[];
        (...items: (string | ConcatArray<string>)[]): string[];
    };
    ... 16 more ...;
    readonly includes: (searchElement: string, fromIndex?: number | undefined) => boolean;
}

For ReadonlyMap:

type B = {
    readonly forEach: (callbackfn: (value: "b", key: "a", map: ReadonlyMap<"a", "b">) => void, thisArg?: any) => void;
    readonly get: (key: "a") => "b" | undefined;
    readonly has: (key: "a") => boolean;
    readonly size: number;
    readonly entries: () => IterableIterator<...>;
    readonly keys: () => IterableIterator<...>;
    readonly values: () => IterableIterator<...>;
}

Solution

Add conditions to check ReadonlyArray and ReadonlyMap in DeepImmutable and Immutable types.