File tree Expand file tree Collapse file tree 2 files changed +25
-22
lines changed
Expand file tree Collapse file tree 2 files changed +25
-22
lines changed Original file line number Diff line number Diff line change @@ -12,28 +12,6 @@ Generic type for deeply immutable data.
1212yarn add @pacote/immutable
1313```
1414
15- ## Usage
16-
17- ``` typescript
18- import type { Immutable } from ' @pacote/immutable'
19-
20- type Foo = { foo: string }
21-
22- const mutable: Foo [] = [{ foo: ' bar' }]
23-
24- // Allowed:
25- mutable .push ({ foo: ' baz' })
26- mutable [0 ].foo = ' baz'
27- delete mutable [0 ].foo
28-
29- const immutable: Immutable <Foo []> = [{ foo: ' bar' }]
30-
31- // Not allowed:
32- immutable .push ({ foo: ' baz' })
33- immutable [0 ].foo = ' baz'
34- delete immutable [0 ].foo
35- ```
36-
3715## License
3816
3917MIT © [ Luís Rodrigues] ( https://goblindegook.com ) .
Original file line number Diff line number Diff line change 1+ /**
2+ * Generic type that converts deeply nested structures into immutable variants.
3+ *
4+ * Useful when you want to treat mutable shapes such as arrays, maps, sets, and
5+ * plain objects as readonly data structures throughout your application.
6+ *
7+ * @example
8+ * import type { Immutable } from '@pacote/immutable'
9+ *
10+ * type Foo = { foo: string }
11+ *
12+ * const mutable: Foo[] = [{ foo: 'bar' }]
13+ *
14+ * // Allowed:
15+ * mutable.push({ foo: 'baz' })
16+ * mutable[0].foo = 'baz'
17+ * delete mutable[0].foo
18+ *
19+ * const immutable: Immutable<Foo[]> = [{ foo: 'bar' }]
20+ *
21+ * // Not allowed:
22+ * immutable.push({ foo: 'baz' })
23+ * immutable[0].foo = 'baz'
24+ * delete immutable[0].foo
25+ */
126export type Immutable < T > = T extends readonly [ infer First , ...infer Rest ]
227 ? readonly [ Immutable < First > , ...ImmutableTuple < Rest > ]
328 : T extends readonly ( infer E ) [ ]
You can’t perform that action at this time.
0 commit comments