API reference / @evolu/common / Function / readonly
Function: readonly()
Defined in: packages/common/src/Function.ts:93
Experimental
Casts an array, set, record, or map to its readonly counterpart.
Zero runtime cost — returns the same value with a readonly type. Use this to enforce immutability at the type level. Preserves NonEmptyArray as NonEmptyReadonlyArray.
Example
// Array literals become NonEmptyReadonlyArray
const items = readonly([1, 2, 3]);
// Type: NonEmptyReadonlyArray<number>
// NonEmptyArray is preserved as NonEmptyReadonlyArray
const nonEmpty: NonEmptyArray<number> = [1, 2, 3];
const readonlyNonEmpty = readonly(nonEmpty);
// Type: NonEmptyReadonlyArray<number>
// Regular arrays become ReadonlyArray
const arr: Array<number> = getNumbers();
const readonlyArr = readonly(arr);
// Type: ReadonlyArray<number>
// Sets, Records, and Maps
const ids = readonly(new Set(["a", "b"]));
// Type: ReadonlySet<string>
const users: Record<UserId, string> = { ... };
const readonlyUsers = readonly(users);
// Type: ReadonlyRecord<UserId, string>
const lookup = readonly(new Map([["key", "value"]]));
// Type: ReadonlyMap<string, string>
Type Parameters
| Type Parameter |
|---|
T |
Parameters
| Parameter | Type |
|---|---|
array | [T, ...T[]] |
Returns
readonly [T, T]
Defined in: packages/common/src/Function.ts:94
Experimental
Casts an array, set, record, or map to its readonly counterpart.
Zero runtime cost — returns the same value with a readonly type. Use this to enforce immutability at the type level. Preserves NonEmptyArray as NonEmptyReadonlyArray.
Example
// Array literals become NonEmptyReadonlyArray
const items = readonly([1, 2, 3]);
// Type: NonEmptyReadonlyArray<number>
// NonEmptyArray is preserved as NonEmptyReadonlyArray
const nonEmpty: NonEmptyArray<number> = [1, 2, 3];
const readonlyNonEmpty = readonly(nonEmpty);
// Type: NonEmptyReadonlyArray<number>
// Regular arrays become ReadonlyArray
const arr: Array<number> = getNumbers();
const readonlyArr = readonly(arr);
// Type: ReadonlyArray<number>
// Sets, Records, and Maps
const ids = readonly(new Set(["a", "b"]));
// Type: ReadonlySet<string>
const users: Record<UserId, string> = { ... };
const readonlyUsers = readonly(users);
// Type: ReadonlyRecord<UserId, string>
const lookup = readonly(new Map([["key", "value"]]));
// Type: ReadonlyMap<string, string>
Type Parameters
| Type Parameter |
|---|
T |
Parameters
| Parameter | Type |
|---|---|
array | T[] |
Returns
readonly T[]
Defined in: packages/common/src/Function.ts:95
Experimental
Casts an array, set, record, or map to its readonly counterpart.
Zero runtime cost — returns the same value with a readonly type. Use this to enforce immutability at the type level. Preserves NonEmptyArray as NonEmptyReadonlyArray.
Example
// Array literals become NonEmptyReadonlyArray
const items = readonly([1, 2, 3]);
// Type: NonEmptyReadonlyArray<number>
// NonEmptyArray is preserved as NonEmptyReadonlyArray
const nonEmpty: NonEmptyArray<number> = [1, 2, 3];
const readonlyNonEmpty = readonly(nonEmpty);
// Type: NonEmptyReadonlyArray<number>
// Regular arrays become ReadonlyArray
const arr: Array<number> = getNumbers();
const readonlyArr = readonly(arr);
// Type: ReadonlyArray<number>
// Sets, Records, and Maps
const ids = readonly(new Set(["a", "b"]));
// Type: ReadonlySet<string>
const users: Record<UserId, string> = { ... };
const readonlyUsers = readonly(users);
// Type: ReadonlyRecord<UserId, string>
const lookup = readonly(new Map([["key", "value"]]));
// Type: ReadonlyMap<string, string>
Type Parameters
| Type Parameter |
|---|
T |
Parameters
| Parameter | Type |
|---|---|
set | Set<T> |
Returns
ReadonlySet<T>
Defined in: packages/common/src/Function.ts:96
Experimental
Casts an array, set, record, or map to its readonly counterpart.
Zero runtime cost — returns the same value with a readonly type. Use this to enforce immutability at the type level. Preserves NonEmptyArray as NonEmptyReadonlyArray.
Example
// Array literals become NonEmptyReadonlyArray
const items = readonly([1, 2, 3]);
// Type: NonEmptyReadonlyArray<number>
// NonEmptyArray is preserved as NonEmptyReadonlyArray
const nonEmpty: NonEmptyArray<number> = [1, 2, 3];
const readonlyNonEmpty = readonly(nonEmpty);
// Type: NonEmptyReadonlyArray<number>
// Regular arrays become ReadonlyArray
const arr: Array<number> = getNumbers();
const readonlyArr = readonly(arr);
// Type: ReadonlyArray<number>
// Sets, Records, and Maps
const ids = readonly(new Set(["a", "b"]));
// Type: ReadonlySet<string>
const users: Record<UserId, string> = { ... };
const readonlyUsers = readonly(users);
// Type: ReadonlyRecord<UserId, string>
const lookup = readonly(new Map([["key", "value"]]));
// Type: ReadonlyMap<string, string>
Type Parameters
| Type Parameter |
|---|
K |
V |
Parameters
| Parameter | Type |
|---|---|
map | Map<K, V> |
Returns
ReadonlyMap<K, V>
Defined in: packages/common/src/Function.ts:97
Experimental
Casts an array, set, record, or map to its readonly counterpart.
Zero runtime cost — returns the same value with a readonly type. Use this to enforce immutability at the type level. Preserves NonEmptyArray as NonEmptyReadonlyArray.
Example
// Array literals become NonEmptyReadonlyArray
const items = readonly([1, 2, 3]);
// Type: NonEmptyReadonlyArray<number>
// NonEmptyArray is preserved as NonEmptyReadonlyArray
const nonEmpty: NonEmptyArray<number> = [1, 2, 3];
const readonlyNonEmpty = readonly(nonEmpty);
// Type: NonEmptyReadonlyArray<number>
// Regular arrays become ReadonlyArray
const arr: Array<number> = getNumbers();
const readonlyArr = readonly(arr);
// Type: ReadonlyArray<number>
// Sets, Records, and Maps
const ids = readonly(new Set(["a", "b"]));
// Type: ReadonlySet<string>
const users: Record<UserId, string> = { ... };
const readonlyUsers = readonly(users);
// Type: ReadonlyRecord<UserId, string>
const lookup = readonly(new Map([["key", "value"]]));
// Type: ReadonlyMap<string, string>
Type Parameters
| Type Parameter |
|---|
K extends string | number | symbol |
V |
Parameters
| Parameter | Type |
|---|---|
record | Record<K, V> |
Returns
ReadonlyRecord<K, V>