Skip to content

Instantly share code, notes, and snippets.

@maxgherman
Created March 15, 2015 12:21
Show Gist options
  • Save maxgherman/3d5901240f15e791116e to your computer and use it in GitHub Desktop.
Save maxgherman/3d5901240f15e791116e to your computer and use it in GitHub Desktop.
Typescript immutable array
class ImmutableArray<T> {
private _data : Array<T>;
public get value() : Array<T>{
return this._data.slice(0);
}
constructor(data : Array<T>) {
if(Array.isArray(data) !== true)
throw new Error('data should represent an array');
this._data = this.initialize(data);
}
private initialize(data : Array<T>) : Array<T> {
return data.map(item => {
var type = typeof (item);
return type === 'object' ||
type === 'function' ? Object.freeze(item) : item;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment