You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hey! I want to suggest freezing empty to avoid occasional mutation (e.g. by third-parties).
Current Behavior
import { empty } from 'fp-ts/lib/Array';
function foo(arr: Array<number>) {
arr.push(1); // might be not that obvious
}
foo(empty); // Neither compile-time nor runtime error
// All the code that depends on `empty` is broken after that
Desired Behavior
import { empty } from 'fp-ts/lib/Array';
function foo(arr: Array<number>) {
arr.push(1);
}
foo(empty); // No compile-time error, but runtime exception
Suggested Solution
const empty: Array<never> = Object.freeze([]);
Who does this impact? Who is this for?
My team several times faced subtle bugs caused by empty been mutated occasionally by third-party code. I think that confidence that empty won't be mutated worth possible runtime exception at development time.
Describe alternatives you've considered
Adding readonly to empty's type - inconveniently restrict possible usages of empty cause you can't pass readonly Array<T> where you expect Array<T>
Object.seal - less strict than Object.freeze, but basically is the same for our case
Doing Object.freeze(empty) in every project - easy to forget about
Your environment
Software
Version(s)
fp-ts
v2.1.0
TypeScript
v3.6.3
The text was updated successfully, but these errors were encountered:
🚀 Feature request
Hey! I want to suggest freezing
empty
to avoid occasional mutation (e.g. by third-parties).Current Behavior
Desired Behavior
Suggested Solution
Who does this impact? Who is this for?
My team several times faced subtle bugs caused by
empty
been mutated occasionally by third-party code. I think that confidence thatempty
won't be mutated worth possible runtime exception at development time.Describe alternatives you've considered
readonly
toempty
's type - inconveniently restrict possible usages ofempty
cause you can't passreadonly Array<T>
where you expectArray<T>
Object.seal
- less strict thanObject.freeze
, but basically is the same for our caseObject.freeze(empty)
in every project - easy to forget aboutYour environment
The text was updated successfully, but these errors were encountered: