|
| 1 | +import { describe, expectTypeOf, it } from 'vitest' |
| 2 | +import type { Immutable } from '../src/index' |
| 3 | + |
| 4 | +describe('Immutable', () => { |
| 5 | + it('marks nested object properties as readonly', () => { |
| 6 | + type Actual = Immutable<{ foo: { bar: string } }> |
| 7 | + type Expected = { readonly foo: { readonly bar: string } } |
| 8 | + expectTypeOf<Actual>().toEqualTypeOf<Expected>() |
| 9 | + }) |
| 10 | + |
| 11 | + it('transforms arrays into readonly arrays', () => { |
| 12 | + type Actual = Immutable<number[]> |
| 13 | + type Expected = readonly number[] |
| 14 | + expectTypeOf<Actual>().toEqualTypeOf<Expected>() |
| 15 | + }) |
| 16 | + |
| 17 | + it('keeps tuple structure while wrapping entries', () => { |
| 18 | + type Actual = Immutable<[number, { label: string }]> |
| 19 | + type Expected = readonly [number, Readonly<{ label: string }>] |
| 20 | + expectTypeOf<Actual>().toEqualTypeOf<Expected>() |
| 21 | + }) |
| 22 | + |
| 23 | + it('wraps map values with immutable variants', () => { |
| 24 | + type Actual = Immutable<Map<{ key: string }, { value: number }>> |
| 25 | + type Expected = ReadonlyMap< |
| 26 | + Readonly<{ key: string }>, |
| 27 | + Readonly<{ value: number }> |
| 28 | + > |
| 29 | + expectTypeOf<Actual>().toEqualTypeOf<Expected>() |
| 30 | + }) |
| 31 | + |
| 32 | + it('wraps set values with immutable variants', () => { |
| 33 | + type Actual = Immutable<Set<{ id: string }>> |
| 34 | + type Expected = ReadonlySet<Readonly<{ id: string }>> |
| 35 | + expectTypeOf<Actual>().toEqualTypeOf<Expected>() |
| 36 | + }) |
| 37 | + |
| 38 | + it('leaves primitive values unchanged', () => { |
| 39 | + type Actual = Immutable<string> |
| 40 | + type Expected = string |
| 41 | + expectTypeOf<Actual>().toEqualTypeOf<Expected>() |
| 42 | + }) |
| 43 | +}) |
0 commit comments