-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Description
say, i have a generic function that by design requires that its parameters may only take immutable arguments, i wish i could get such guarantee from TS by declaring my function as follows:
function doThings<readonly T>(value: T): Result<T> {
// ...
}
as far as how to make an interface immutable:
readonly interface Point {
readonly x: number;
readonly y: number;
}
so the problem being solve here is to make sure that the caller won't mutate the argument after it is passed to the function
simple use case: a hash-based data container, which calculates a hash of a given value, and will store it at that hash, and it all will work until the value is mutated outside and tha stored hash is no longer valid, so the container doesn't really work anymore
another use case: a cache or object pool or any other situation when there are many parties involved in taking hold of the same object, which must be guaranteed from being mutated by one party to prevent spooky action at a distance for all other parties