UNPKG

2.08 kBTypeScriptView Raw
1/**
2 * @license
3 * Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
4 * This code may only be used under the BSD style license found at
5 * http://polymer.github.io/LICENSE.txt
6 * The complete set of authors may be found at
7 * http://polymer.github.io/AUTHORS.txt
8 * The complete set of contributors may be found at
9 * http://polymer.github.io/CONTRIBUTORS.txt
10 * Code distributed by Google as part of the polymer project is also
11 * subject to an additional IP rights grant found at
12 * http://polymer.github.io/PATENTS.txt
13 */
14/**
15 * This is a simple pattern to provide a readonly view of a normal javascript
16 * collection. Note that this trades performance for protection. There's nothing
17 * stoping a user from mutating one of these data structures, save a warning
18 * that the typescript compiler may give them.
19 *
20 * Other options to consider are facebook's Immutable.js which provides a
21 * variety of truely immutable data structures.
22 */
23/** A zero-overhead immutable view of an array. */
24export declare type ImmutableArray<V> = ReadonlyArray<V>;
25/** A zero-overhead immutable view of a set. */
26export declare type ImmutableSet<V> = ReadonlySet<V>;
27/** A zero-overhead immutable view of a map. */
28export declare type ImmutableMap<K, V> = ReadonlyMap<K, V>;
29export declare function asImmutable<V>(array: Array<V>): ImmutableArray<V>;
30export declare function asImmutable<V>(set: Set<V>): ImmutableSet<V>;
31export declare function asImmutable<K, V>(map: Map<K, V>): ImmutableMap<K, V>;
32export declare function asImmutable<O extends object>(object: O): Readonly<O>;
33/**
34 * Take care, this function is inherently unsafe.
35 *
36 * You're taking a data structure that has been declare as immutable and getting
37 * a mutable reference to it.
38 */
39export declare function unsafeAsMutable<V>(array: ImmutableArray<V>): Array<V>;
40export declare function unsafeAsMutable<V>(set: ImmutableSet<V>): Set<V>;
41export declare function unsafeAsMutable<K, V>(map: ImmutableMap<K, V>): Map<K, V>;
42export declare function unsafeAsMutable<O extends object>(object: Readonly<O>): O;